GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 08-Oct-2008, 19:05
razzar razzar is offline
New Member
 
Join Date: Oct 2008
Posts: 2
razzar is on a distinguished road

Define the entry point for a console application


Hello. Im trying tom make a number game and need some help. as seen in the code bellow it loads a few numbers from "start.txt". from start.txt it loads the numbers "1,2,3,4,5,6,7,8,X" and places it in a 3*3 grid. what i need some help with is X. i need to be able to move around around X inside the grid with "W A S D" but cant figure out how to do it can someone help me or make an example on how i can accomplish this ?

CPP / C++ / C Code:
// heero.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>  
#include <fstream>
#include <string>
using namespace std; 

void jump(char start[][3]);

int main() 
{
	int yes,abryt;
char start[3][3], goal[3][3];

ifstream infil("start.txt");

infil >> start[0][0] >> start[0][1] >> start[0][2] >> start[1][0] >> start[1][1] >> start[1][2] >> start[2][0] >> start[2][1] >> start[2][2];
cout << start[0][0] << " " << start[0][1] << " " << start[0][2] << endl << start[1][0] << " " << start[1][1] << " " << start[1][2] << endl << start[2][0] << " " << start[2][1] << " " << start[2][2] << endl << endl;

jump(start);

return 0;
}

void jump(char start[][3]){
string jump2;
cout << "You move by using W A S D on you keyboard";
cin >> jump2;

if(jump2 == "avbryt"){
	string quit;
    cout << "Want to quit the game eh? Yes/no" ;
    cin >> quit;
if(quit == "yes"){
	cout << "hejdå medellande";
}
else cout << "kvar medellande";

}

if(jump2 == "W"){
	/* Here*/
}
if(jump2 == "A"){
	/* Here*/
}
if(jump2 == "S"){
	/* Here*/
}
if(jump2 == "D"){
	/* Here*/

}
}
Last edited by admin : 08-Oct-2008 at 23:01. Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags
  #2  
Old 08-Oct-2008, 19:29
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 580
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: Need some newbie guidence


Quote:
Originally Posted by razzar
i need to be able to move around around X inside the grid with "W A S D" but cant figure out how to do it can someone help me or make an example on how i can accomplish this ?
From your description, I can only assume that 'X' is to be treated as a cursor which is moved about the matrix.
Quote:
it loads the numbers "1,2,3,4,5,6,7,8,X"
Once 'X' is moved, the element it occupied is considered empty?

My suggestion in creating the logic for cursor movement is to track the location of 'X' by its (row, column) position. The logic you need to implement is:
  • When pressing whatever character represents "up", decrement the row number of the cursor.
  • If the keypress denotes "down", increment the row number of the cursor.
  • If the keypress denotes, "right", increment the cursor's column number.
  • etc.
This raises the obvious question as to what to do when "down" is pressed, & the cursor is already at the bottom of the matrix. You have two choices:
  • Do nothing.
  • When incrementing & decrementing, use modulo arithmetic. In this fashion, pressing "down" when the cursor is already at the bottom of the matrix moves the cursor to the top row.
Note that whenever incrementing or decrementing occurs, you will need to overwrite the new & old matrix elements. Whether you should be saving the contents the cursor is overwriting is up to you. I don't know the rules to your game.
  #3  
Old 09-Oct-2008, 10:09
razzar razzar is offline
New Member
 
Join Date: Oct 2008
Posts: 2
razzar is on a distinguished road

Re: Need some newbie guidence


Quote:
Originally Posted by ocicat
From your description, I can only assume that 'X' is to be treated as a cursor which is moved about the matrix.

Once 'X' is moved, the element it occupied is considered empty?

My suggestion in creating the logic for cursor movement is to track the location of 'X' by its (row, column) position. The logic you need to implement is:
  • When pressing whatever character represents "up", decrement the row number of the cursor.
  • If the keypress denotes "down", increment the row number of the cursor.
  • If the keypress denotes, "right", increment the cursor's column number.
  • etc.
This raises the obvious question as to what to do when "down" is pressed, & the cursor is already at the bottom of the matrix. You have two choices:
  • Do nothing.
  • When incrementing & decrementing, use modulo arithmetic. In this fashion, pressing "down" when the cursor is already at the bottom of the matrix moves the cursor to the top row.
Note that whenever incrementing or decrementing occurs, you will need to overwrite the new & old matrix elements. Whether you should be saving the contents the cursor is overwriting is up to you. I don't know the rules to your game.


Exactly! but when X is moved to a position wich a number in it it's supposed to switch places, so lets say i moved X to the possition above it that would hold the number 6 they switch places so 6 is moved to where X where and vice e versa. and your cursor sounds like what i need yes, but how can i accomplish this? cirrently, i have no clue!
  #4  
Old 09-Oct-2008, 11:57
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 580
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: Need some newbie guidence


Quote:
Originally Posted by razzar
...how can i accomplish this?
  1. Get a keypress.
  2. Determine whether the keypress is valid or not.
  3. If valid, get the coordinates of where 'X' is currently.
  4. Compute where 'X' should be moved, & save the contents of this new position.
  5. Exchange the value of the matrix at the new position with where 'X' was previously.
  6. Save the new position where 'X' is now.
  7. Redisplay the board.
  8. Get another keypress.
From this list, you should see that the processing will be done in a loop -- most likely, a while-loop. You will also want to designate some specific character or set of characters as being the key pressed to exit the game (& loop).

One thing you have not asked or mentioned is how you will be displaying the board. If this to be done at the console in text-based graphics:
  • Search through your compiler's documentation to see if you have some form of gotoxy() type function. Sometimes vendors put this kind of functionality in conio.h. Be forewarned that this type of functionality is rarely portable from compiler to compiler or platform to platform.
  • Use the curses library which provides the same functionality. Some compiler vendors include this with their compiler, most don't. I will leave it to you to search Google for information on this library, & whether it can be integrated with your compiler.
  • If you intend to draw your board graphically within Windows or X Windows, I will leave to you to search for how to do it.
Personally, worrying about how to display the board in a finished form is not the important question at this moment. Simply write a simple function which dumps out the contents of the 3x3 matrix.

At this point, you should be developing the basic engine described above, & ensuring that you get the expected behaviour upon any keypress.
  #5  
Old 09-Oct-2008, 12:16
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 803
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

Re: Define the entry point for a console application


Right, reprinting the grid and letting things scroll up would be the simplest for now
but if you decide to get fancy here are some links that helped me learn how to 'handle' the screen:
MS : microsoft.com/en-us/library/ms682010(VS.85).aspx , adrianxw.dk/SoftwareSite/Consoles/Consoles1.html
or in Linux 'ncurses' : tldp.org/HOWTO/NCURSES-Programming.html
But keep in mind that this forum is for questions regarding the languages themselves.
Graphical programming is a different subject and so you won't get much help on that.
 
 

Recent GIDBlogToyota - 2009 May Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Need review of Python Tutorial Book for newbie agiduser Python Forum 4 29-Feb-2008 16:41
Newbie questions regarding inputing a sentence Emir C Programming Language 2 10-Dec-2007 10:26
Newbie : need help with Dev-C++ compiler batrsau C++ Forum 2 20-Mar-2005 22:05
linked lists, newbie needs help moltarim C Programming Language 4 06-May-2004 12:32
newbie using Sam's series Skampy C++ Forum 3 06-Mar-2004 19:51

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 20:51.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.