See anything wrong?
I am makin a tic-tac-toe game... i would post the code, but its on another comp...
for some reason it is not winning a win situation correctly...
see if you find anything wrong with this statement.
if((board[0] == board[1]) && (board[1] == board[2]) && (board[2] == p1)) { cout << "player 1 wins";}
there are 7 other statements like the one above testing for diagonals and other rows and such
what i did was set the board of as an integer array of 9 elements. I initialized them all to 0. If a player moved in that spot, it would change into a 1 or a 2, depending on the player.
it compiles fine... and seems to run properly at first, but determines that a player won too early.
For instance... if I put an X in the top right position
(board[2]), it says i won.... anyone see why from this ?
If barbie is so popular, why do you have to buy her friends?
If barbie is so popular, why do you have to buy her friends?
I think we''re definitely going to need more of the code to see exactly what''s going wrong.
http://www.gamedev.net/community/gds/projects/default.asp?projectID=205
I started with tic-tac-to as well, and used a similar method for determining victory. If you have the logic background, try making a karnough map to solve for winning situations. Or just look at the code.
To debug your code, add a break point after that line of code should have executed and check to see that all of your variables are what you think they are.
Visit my web site.
"I came, I saw, I coded."
I started with tic-tac-to as well, and used a similar method for determining victory. If you have the logic background, try making a karnough map to solve for winning situations. Or just look at the code.
To debug your code, add a break point after that line of code should have executed and check to see that all of your variables are what you think they are.
Visit my web site.
"I came, I saw, I coded."
Visit my web site."I came, I saw, I coded."
That line of code appears to be correct. Please post the rest of the little algorithm here.
here is the rest of the code... its pretty sloppy, i didn''t do any planning at all, just got the notion to randomly write it at 12:30 last night (this morning), and did this in 30 mins or so. The A.I. is not complete, but i had put in enough code to test simple winning, and that didn''t go well... see what u think
*************************************************************
#include <iostream.h>
int threat();
move_ai();
place_row1();
place_row2();
place_row3();
check_win();
move_human();
int threat_position;
int p1=1;
int comp=2;
int pos=0;
int board[9] = {0,0,0,
0,0,0,
0,0,0};
//////////////////////////////// MAIN ///////////////////////////////////////
int main()
{
cout<<"I''m so confident that I can beat you, i''ll let you go first...\n\n\n";
while(check_win())
{
move_human();
if(check_win)
{
move_ai();
}
}
return 0;
}
///////////////////////////END OF MAIN//////////////////////////////
move_human()
{
int move_to;
cout << "\n\n where would you like to move? ";
cin >> move_to;
board[move_to] = p1;
}
move_ai()
//structured poorly but just kinda messing, making him impossible to beat basically...
//the order checked to make unbeatable... not matter who goes first (i think)
/*
1) go in middle if it is open
2) if you have two in one row, go there
3) if they have two in one row, go there
4) if he has two corners, go in the middle of a side
5) Go in a corner
6) game will pretty much be a cat game.. .fill in a space
*/
{
threat_position = threat();
if(board[4] == 0)
{
board[4] = comp;
}
else if(threat())
{
switch(threat_position)
{
case 1:
case 2:
place_row1();
break;
case 4:
case 5:
place_row2();
break;
case 7:
case 8:
place_row3();
break;
}
}
}
int threat()
{
for(int count=0; count < 3; count++)
{
for(int count2 =count*3; count2 < (3+3*count); count2++)
{
if (board[count2] = p1)
{
pos++;
}
if(pos==2)
{
return count2;
}
}
pos=0;
}
return 0;
}
place_row1()
{
if(board[0] == 0)
{
board[0] = comp;
}
else if(board[1] == 0)
{
board[1] = comp;
}
else if(board[2] == 0)
{
board[2] = comp;
}
}
place_row2()
{
if(board[3] == 0)
{
board[3] = comp;
}
else if(board[4] == 0)
{
board[4] = comp;
}
else if(board[5] == 0)
{
board[5] = comp;
}
}
place_row3()
{
if(board[6] == 0)
{
board[6] = comp;
}
else if(board[7] == 0)
{
board[7] = comp;
}
else if(board[8] == 0)
{
board[8] = comp;
}
}
int check_win()
{int win=1;
if((board[0] == board[1]) && (board[1] == board[2]) && (board[2] == p1)) {cout << "p1 wins\n";win = 0;}
if((board[3] == board[4]) && (board[4] == board[5]) && (board[5] == p1)) {cout << "p1 wins\n";win = 0;}
if((board[6] == board[7]) && (board[7] == board[8]) && (board[8] == p1)) {cout << "p1 wins\n";win = 0;}
if((board[0] == board[3]) && (board[3] == board[6]) && (board[6] == p1)) {cout << "p1 wins\n";win = 0;}
if((board[1] == board[4]) && (board[4] == board[7]) && (board[7] == p1)) {cout << "p1 wins\n";win = 0;}
if((board[2] == board[5]) && (board[5] == board[8]) && (board[8] == p1)) {cout << "p1 wins\n";win = 0;}
if((board[0] == board[4]) && (board[4] == board[8]) && (board[8] == p1)) {cout << "p1 wins\n";win = 0;}
if((board[2] == board[4]) && (board[4] == board[6]) && (board[6] == p1)) {cout << "p1 wins\n";win = 0;}
if((board[0] == board[1]) && (board[1] == board[2]) && (board[2] == comp)) {cout << "comp wins\n";win = 0;}
if((board[3] == board[4]) && (board[4] == board[5]) && (board[5] == comp)) {cout << "comp wins\n";win = 0;}
if((board[6] == board[7]) && (board[7] == board[8]) && (board[8] == comp)) {cout << "comp wins\n";win = 0;}
if((board[0] == board[3]) && (board[3] == board[6]) && (board[6] == comp)) {cout << "comp wins\n";win = 0;}
if((board[1] == board[4]) && (board[4] == board[7]) && (board[7] == comp)) {cout << "comp wins\n";win = 0;}
if((board[2] == board[5]) && (board[5] == board[8]) && (board[8] == comp)) {cout << "comp wins\n";win = 0;}
if((board[0] == board[4]) && (board[4] == board[8]) && (board[8] == comp)) {cout << "comp wins\n";win = 0;}
if((board[2] == board[4]) && (board[4] == board[6]) && (board[6] == comp)) {cout << "comp wins\n";win = 0;}
return win;
}
**************************************************
If barbie is so popular, why do you have to buy her friends?
*************************************************************
#include <iostream.h>
int threat();
move_ai();
place_row1();
place_row2();
place_row3();
check_win();
move_human();
int threat_position;
int p1=1;
int comp=2;
int pos=0;
int board[9] = {0,0,0,
0,0,0,
0,0,0};
//////////////////////////////// MAIN ///////////////////////////////////////
int main()
{
cout<<"I''m so confident that I can beat you, i''ll let you go first...\n\n\n";
while(check_win())
{
move_human();
if(check_win)
{
move_ai();
}
}
return 0;
}
///////////////////////////END OF MAIN//////////////////////////////
move_human()
{
int move_to;
cout << "\n\n where would you like to move? ";
cin >> move_to;
board[move_to] = p1;
}
move_ai()
//structured poorly but just kinda messing, making him impossible to beat basically...
//the order checked to make unbeatable... not matter who goes first (i think)
/*
1) go in middle if it is open
2) if you have two in one row, go there
3) if they have two in one row, go there
4) if he has two corners, go in the middle of a side
5) Go in a corner
6) game will pretty much be a cat game.. .fill in a space
*/
{
threat_position = threat();
if(board[4] == 0)
{
board[4] = comp;
}
else if(threat())
{
switch(threat_position)
{
case 1:
case 2:
place_row1();
break;
case 4:
case 5:
place_row2();
break;
case 7:
case 8:
place_row3();
break;
}
}
}
int threat()
{
for(int count=0; count < 3; count++)
{
for(int count2 =count*3; count2 < (3+3*count); count2++)
{
if (board[count2] = p1)
{
pos++;
}
if(pos==2)
{
return count2;
}
}
pos=0;
}
return 0;
}
place_row1()
{
if(board[0] == 0)
{
board[0] = comp;
}
else if(board[1] == 0)
{
board[1] = comp;
}
else if(board[2] == 0)
{
board[2] = comp;
}
}
place_row2()
{
if(board[3] == 0)
{
board[3] = comp;
}
else if(board[4] == 0)
{
board[4] = comp;
}
else if(board[5] == 0)
{
board[5] = comp;
}
}
place_row3()
{
if(board[6] == 0)
{
board[6] = comp;
}
else if(board[7] == 0)
{
board[7] = comp;
}
else if(board[8] == 0)
{
board[8] = comp;
}
}
int check_win()
{int win=1;
if((board[0] == board[1]) && (board[1] == board[2]) && (board[2] == p1)) {cout << "p1 wins\n";win = 0;}
if((board[3] == board[4]) && (board[4] == board[5]) && (board[5] == p1)) {cout << "p1 wins\n";win = 0;}
if((board[6] == board[7]) && (board[7] == board[8]) && (board[8] == p1)) {cout << "p1 wins\n";win = 0;}
if((board[0] == board[3]) && (board[3] == board[6]) && (board[6] == p1)) {cout << "p1 wins\n";win = 0;}
if((board[1] == board[4]) && (board[4] == board[7]) && (board[7] == p1)) {cout << "p1 wins\n";win = 0;}
if((board[2] == board[5]) && (board[5] == board[8]) && (board[8] == p1)) {cout << "p1 wins\n";win = 0;}
if((board[0] == board[4]) && (board[4] == board[8]) && (board[8] == p1)) {cout << "p1 wins\n";win = 0;}
if((board[2] == board[4]) && (board[4] == board[6]) && (board[6] == p1)) {cout << "p1 wins\n";win = 0;}
if((board[0] == board[1]) && (board[1] == board[2]) && (board[2] == comp)) {cout << "comp wins\n";win = 0;}
if((board[3] == board[4]) && (board[4] == board[5]) && (board[5] == comp)) {cout << "comp wins\n";win = 0;}
if((board[6] == board[7]) && (board[7] == board[8]) && (board[8] == comp)) {cout << "comp wins\n";win = 0;}
if((board[0] == board[3]) && (board[3] == board[6]) && (board[6] == comp)) {cout << "comp wins\n";win = 0;}
if((board[1] == board[4]) && (board[4] == board[7]) && (board[7] == comp)) {cout << "comp wins\n";win = 0;}
if((board[2] == board[5]) && (board[5] == board[8]) && (board[8] == comp)) {cout << "comp wins\n";win = 0;}
if((board[0] == board[4]) && (board[4] == board[8]) && (board[8] == comp)) {cout << "comp wins\n";win = 0;}
if((board[2] == board[4]) && (board[4] == board[6]) && (board[6] == comp)) {cout << "comp wins\n";win = 0;}
return win;
}
**************************************************
If barbie is so popular, why do you have to buy her friends?
If barbie is so popular, why do you have to buy her friends?
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement