Jul 30, 2009 at 11:37pm Jul 30, 2009 at 11:37pm UTC
i have started a tic tac toe game. i can move the x around with the arrow keys.i dont know how to make it stay there when enter is pressed then create a new one. im using opengl if that makes a difference. thanks for the help.
Jul 31, 2009 at 1:05am Jul 31, 2009 at 1:05am UTC
i have figured out that i need x's on every one that are the same colour as the background and when the arrows are pressed it makes them white. but when the user presses enter how would it make it stay when the arrows are pressed again?
Jul 31, 2009 at 1:20am Jul 31, 2009 at 1:20am UTC
Why aren't you just dynamically creating an X object at the cell on keydown and setting its coords to whatever the cell is?
Jul 31, 2009 at 2:48am Jul 31, 2009 at 2:48am UTC
how would i get it to stay when the user presses enter then create a new one in a blank space.
Jul 31, 2009 at 3:26am Jul 31, 2009 at 3:26am UTC
Brainstorming here:
Perhaps an array like this:
char marks[3][3];
Then you can set the position of the array to 'X' or 'O', depending on where the mark is. When you draw the board, add a step that goes through the array and draws any letters it encounters.
Remember, I'm brainstorming, I haven't actually tested it and it might need to be tweaked to work.
Last edited on Jul 31, 2009 at 3:26am Jul 31, 2009 at 3:26am UTC
Jul 31, 2009 at 11:20am Jul 31, 2009 at 11:20am UTC
I only have experience with console applications, but recently I did a similar program with a 9x9 grid. Use <space> to place the 'X' or 'O' where you want it, and 'W', 'A','S' and 'D' to move the pointer, it is posted below, hope it helps. I have not looked at it for sometime, so you probably have to figure out the code yourself.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
#include <iostream>
#include <conio.h> //get character Console
using namespace std;
//class for boxes
class box
{
public :
box();
char value,disp;
};
box::box()
{
value='.' ;
disp='.' ;
}
//initialize boxes for TICTACTOE
box box1[9][9];
char user,user1,user2;
//function to check boxes
void check_box(void )
{
if (user==user1) cout<<"Player1" ;
else if (user==user2) cout<<"Player2" ;
cout<<endl<<endl;
for (int x=0;x<9;x++)
{
for (int y=0;y<9;y++)
cout<<" " <<box1[x][y].disp;
cout<<endl;
}
cout<<"\n\n\n" ;
}
//define user symbol
void user_symbol(void )
{
user1 = '.' ;
user2 = '.' ;
cout<<"Define symbol for user1 (X or O) : " ;
while (user1!='x' &&user1!='X' &&user1!='o' &&user1!='O' )
{
cin>>user1;
if (user1!='x' &&user1!='X' &&user1!='o' &&user1!='O' ) cout<<"ERROR! Define symbol for user1 (x or o) : " ;
}
if (user1=='x' ||user1=='X' )
{
user1='x' ;
user2='o' ;
}
else
{
user1='o' ;
user2='x' ;
}
cout<<"\n\nSymbol for user1 will be " << user1 <<endl;
cout<<"Symbol for user2 will be " << user2 <<endl;
}
void init_cursor(short x,short y)//short half a machine word
{
box1[x][y].disp='W' ;
}
short check_game(void )
{
short x_start,y_start;
bool status=0;
for (y_start=0;y_start<9;y_start++)
{
for (x_start=0;x_start<9;x_start++)
{
for (int direction=0;direction<4;direction++)
{
switch (direction)
{
case 0://towards down
if (box1[x_start][y_start].value==box1[x_start+1][y_start].value&&box1[x_start][y_start].value==box1[x_start+2][y_start].value&&box1[x_start][y_start].value==box1[x_start+3][y_start].value&&box1[x_start][y_start].value==box1[x_start+4][y_start].value)
if (box1[x_start][y_start].value!='.' ) status=1;
break ;
case 1://towards right
if (box1[x_start][y_start].value==box1[x_start][y_start+1].value&&box1[x_start][y_start].value==box1[x_start][y_start+2].value&&box1[x_start][y_start].value==box1[x_start][y_start+3].value&&box1[x_start][y_start].value==box1[x_start][y_start+4].value)
if (box1[x_start][y_start].value!='.' ) status=1;
break ;
case 2://towards bot right
if (box1[x_start][y_start].value==box1[x_start+1][y_start+1].value&&box1[x_start][y_start].value==box1[x_start+2][y_start+2].value&&box1[x_start][y_start].value==box1[x_start+3][y_start+3].value&&box1[x_start][y_start].value==box1[x_start+4][y_start+4].value)
if (box1[x_start][y_start].value!='.' ) status=1;
break ;
case 3:
if (box1[x_start][y_start].value==box1[x_start+1][y_start-1].value&&box1[x_start][y_start].value==box1[x_start+2][y_start-2].value&&box1[x_start][y_start].value==box1[x_start+3][y_start-3].value&&box1[x_start][y_start].value==box1[x_start+4][y_start-4].value)
if (box1[x_start][y_start].value!='.' ) status=1;
break ;
}
}
}
}
return status;
}
//main function
int main()
{
bool game_end;
short x,y;
char key;
x=4;y=4;
// key=getch();
// cout<<key;
user_symbol();
user=user1;
init_cursor(x,y);
check_box();
while (!game_end)
{
key=getch();
if ((key=='w' ||key=='W' )&&(x>0))
{
box1[x][y].disp=box1[x][y].value;
x--;
}
else if ((key=='s' ||key=='S' )&&(x<8))
{
box1[x][y].disp=box1[x][y].value;
x++;
}
else if ((key=='a' ||key=='A' )&&(y>0))
{
box1[x][y].disp=box1[x][y].value;
y--;
}
else if ((key=='d' ||key=='D' )&&(y<8))
{
box1[x][y].disp=box1[x][y].value;
y++;
}
else if (key==' ' &&box1[x][y].value=='.' )
{
box1[x][y].value=user;
if (user==user1) user=user2;
else user=user1;
}
else if (key==27)//27 the "ESC"
{
game_end=1;
}
init_cursor(x,y);
check_box();
if (check_game()==1)
{
game_end=1;
if (user==user1) user=user2;
else user=user1;
cout << user << " WINS THE GAME!! Congratulations!!\nThank you for playing this game." << endl;
}
}
return 0;
}
Hope it helps.
P.S. I'm not sure why my post box is stretched so far right, sorry about that.
Last edited on Jul 31, 2009 at 11:21am Jul 31, 2009 at 11:21am UTC