Tic-Tac-Toe error

I wrote a tic-tac-toe game and it works good except the program returns 88 intead of X and i don't see why.

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
#include "simpio.h"
#include <stdio.h>
char matrix[3][3];//={0};
void cou(void);
int main()
{
printf("When you want to quit press ctrl+c\n");
int m,n;
char ch='y';
char again;
while ((again=getchar()) != EOF)
{
while(ch=='Y'||ch=='y'){
for (m=0;m<3;m++)for (n=0;n<3;n++)matrix[m][n]= '\0';
int i,j,sum=0;
while ( sum < 10){
if (sum == 0) cou();
printf("Player 1 is 'X': choose the row and column");
printf("\nRow : ");
i=GetInteger();
printf("\nColumn : ");
j=GetInteger();
for (; i>3 || i<1 || j>3 || j<1 ||('X'==matrix[i-1][j-1]||'O'==matrix[i-1][j-1]);) 
{
    printf("Sorry, but you gotta choose another place.\n");
    printf("row : ");
    i=GetInteger();
    printf("column : ");
    j=GetInteger();
    }
matrix[i-1][j-1]='X';
sum++;
cou();

//check if wins
if (matrix[0][0]=='X' && matrix[0][0]==matrix[1][1] && matrix[1][1]==matrix[2][2]) {printf("Player 1 wins");break;}
if (matrix[2][0]=='X' && matrix[2][0]==matrix[1][1] && matrix[1][1]==matrix[0][2]) {printf("Player 1 wins");break;}
if (matrix[0][0]=='X' && matrix[0][0]==matrix[1][0] && matrix[1][0]==matrix[2][0]) {printf("Player 1 wins");break;}
if (matrix[0][1]=='X' && matrix[0][1]==matrix[1][1] && matrix[1][1]==matrix[2][1]) {printf("Player 1 wins");break;}
if (matrix[0][2]=='X' && matrix[0][2]==matrix[1][2] && matrix[1][2]==matrix[2][2]) {printf("Player 1 wins");break;}
if (matrix[0][0]=='X' && matrix[0][0]==matrix[0][1] && matrix[0][1]==matrix[0][2]) {printf("Player 1 wins");break;}
if (matrix[1][0]=='X' && matrix[1][0]==matrix[1][1] && matrix[1][1]==matrix[1][2]) {printf("Player 1 wins");break;}
if (matrix[2][0]=='X' && matrix[2][0]==matrix[2][1] && matrix[2][1]==matrix[2][2]) {printf("Player 1 wins");break;}

if (sum == 9){printf("The game is over and no one wins"); break;} //sum=9 because there are only 9 boxes in the game
//player 2's turn

printf("Player 2 is 'O': choose the row and column");
printf("Row : ");
i=GetInteger();
printf("Column : ");
j=GetInteger();
for (;i>3 || i<1 || j>3 || j<1 ||('X'==matrix[i-1][j-1]||'O'==matrix[i-1][j-1]);) 
{
printf("Sorry, but you gotta choose another place.\n");printf("row : ");i=GetInteger();printf("column : ");j=GetInteger();}
matrix[i-1][j-1]='O';
sum++;
//the play box
cou();
//check if wins
if (matrix[0][0]=='O' && matrix[0][0]==matrix[1][1] && matrix[1][1]==matrix[2][2]) {printf("Player 2 wins");break;}
if (matrix[2][0]=='O' && matrix[2][0]==matrix[1][1] && matrix[1][1]==matrix[0][2]) {printf("Player 2 wins");break;}
if (matrix[0][0]=='O' && matrix[0][0]==matrix[1][0] && matrix[1][0]==matrix[2][0]) {printf("Player 2 wins");break;}
if (matrix[0][1]=='O' && matrix[0][1]==matrix[1][1] && matrix[1][1]==matrix[2][1]) {printf("Player 2 wins");break;}
if (matrix[0][2]=='O' && matrix[0][2]==matrix[1][2] && matrix[1][2]==matrix[2][2]) {printf("Player 2 wins");break;}
if (matrix[0][0]=='O' && matrix[0][0]==matrix[0][1] && matrix[0][1]==matrix[0][2]) {printf("Player 2 wins");break;}
if (matrix[1][0]=='O' && matrix[1][0]==matrix[1][1] && matrix[1][1]==matrix[1][2]) {printf("Player 2 wins");break;}
if (matrix[2][0]=='O' && matrix[2][0]==matrix[2][1] && matrix[2][1]==matrix[2][2]) {printf("Player 2 wins");break;}

}
printf("\nWould you like to play again??? (Y - N)\n");
ch=getchar();
}
      getchar();
      return 0;
}
}
void cou(void)
{
//the play box
printf("\n\t\t                1   2   3\n");
printf("\t\t             1  %d | %d | %d\n", matrix[0][0],matrix[0][1],matrix[0][2]); 
printf("\t\t               ---|---|---\n"); 
printf("\t\t             2  %d | %d | %d\n", matrix[1][0],matrix[1][1],matrix[1][2]);
printf("\t\t               ---|---|---\n");         
printf("\t\t             3  %d | %d | %d\n", matrix[2][0],matrix[2][1],matrix[2][2]);
}
printf() specifier %d will print an integer or decimal. you want the specifier %s
Thanks it works now.
No problem :)
Hi, I have a question. How would i turn the Tic-Tac-Toe program that i wrote which requires 2 human players into a program that only 1 human player and a computer player?

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
#include "simpio.h"
#include <stdio.h>
char matrix[3][3];//={0};
void board(void);
int main()
{
int m,n;
char ch='y';
char x='X';
char o='O';
char again;
while(ch=='Y'||ch=='y'){
for (m=0;m<3;m++)for (n=0;n<3;n++)matrix[m][n]= '\0';
int i,j,sum=0;
while ( sum < 10){
if (sum == 0) board();
printf("Player 1 is 'X': choose the row and column");
printf("\nRow : ");
i=GetInteger();
printf("\nColumn : ");
j=GetInteger();
for (; i>3 || i<1 || j>3 || j<1 ||('X'==matrix[i-1][j-1]||'O'==matrix[i-1][j-1]);) 
{
    printf("Sorry, but you gotta choose another place.\n");
    printf("row : ");
    i=GetInteger();
    printf("column : ");
    j=GetInteger();
    }
matrix[i-1][j-1]='X';
sum++;
board();

//check if wins
if (matrix[0][0]=='X' && matrix[0][0]==matrix[1][1] && matrix[1][1]==matrix[2][2]) {printf("Player 1 wins");break;}
if (matrix[2][0]=='X' && matrix[2][0]==matrix[1][1] && matrix[1][1]==matrix[0][2]) {printf("Player 1 wins");break;}
if (matrix[0][0]=='X' && matrix[0][0]==matrix[1][0] && matrix[1][0]==matrix[2][0]) {printf("Player 1 wins");break;}
if (matrix[0][1]=='X' && matrix[0][1]==matrix[1][1] && matrix[1][1]==matrix[2][1]) {printf("Player 1 wins");break;}
if (matrix[0][2]=='X' && matrix[0][2]==matrix[1][2] && matrix[1][2]==matrix[2][2]) {printf("Player 1 wins");break;}
if (matrix[0][0]=='X' && matrix[0][0]==matrix[0][1] && matrix[0][1]==matrix[0][2]) {printf("Player 1 wins");break;}
if (matrix[1][0]=='X' && matrix[1][0]==matrix[1][1] && matrix[1][1]==matrix[1][2]) {printf("Player 1 wins");break;}
if (matrix[2][0]=='X' && matrix[2][0]==matrix[2][1] && matrix[2][1]==matrix[2][2]) {printf("Player 1 wins");break;}

if (sum == 9){printf("The game is over and no one wins"); break;} //sum=9 because there are only 9 boxes in the game
//player 2's turn

printf("Player 2 is 'O': choose the row and column");
printf("Row : ");
i=GetInteger();
printf("Column : ");
j=GetInteger();
for (;i>3 || i<1 || j>3 || j<1 ||('X'==matrix[i-1][j-1]||'O'==matrix[i-1][j-1]);) 
{
printf("Sorry, but you gotta choose another place.\n");printf("row : ");i=GetInteger();printf("column : ");j=GetInteger();}
matrix[i-1][j-1]=o;
sum++;
//the play box
board();
//check if wins
if (matrix[0][0]=='O' && matrix[0][0]==matrix[1][1] && matrix[1][1]==matrix[2][2]) {printf("Player 2 wins");break;}
if (matrix[2][0]=='O' && matrix[2][0]==matrix[1][1] && matrix[1][1]==matrix[0][2]) {printf("Player 2 wins");break;}
if (matrix[0][0]=='O' && matrix[0][0]==matrix[1][0] && matrix[1][0]==matrix[2][0]) {printf("Player 2 wins");break;}
if (matrix[0][1]=='O' && matrix[0][1]==matrix[1][1] && matrix[1][1]==matrix[2][1]) {printf("Player 2 wins");break;}
if (matrix[0][2]=='O' && matrix[0][2]==matrix[1][2] && matrix[1][2]==matrix[2][2]) {printf("Player 2 wins");break;}
if (matrix[0][0]=='O' && matrix[0][0]==matrix[0][1] && matrix[0][1]==matrix[0][2]) {printf("Player 2 wins");break;}
if (matrix[1][0]=='O' && matrix[1][0]==matrix[1][1] && matrix[1][1]==matrix[1][2]) {printf("Player 2 wins");break;}
if (matrix[2][0]=='O' && matrix[2][0]==matrix[2][1] && matrix[2][1]==matrix[2][2]) {printf("Player 2 wins");break;}

}
printf("\nWould you like to play again??? (Y - N)\n");
ch=getchar();
}
      getchar();
      return 0;
}
void board(void)
{
//the play box
printf("\n\t\t                1   2   3\n");
printf("\t\t             1  %c | %c | %c\n", matrix[0][0],matrix[0][1],matrix[0][2]); 
printf("\t\t               ---|---|---\n"); 
printf("\t\t             2  %c | %c | %c\n", matrix[1][0],matrix[1][1],matrix[1][2]);
printf("\t\t               ---|---|---\n");         
printf("\t\t             3  %c | %c | %c\n", matrix[2][0],matrix[2][1],matrix[2][2]);
}
You're getting into AI programming now. I'd suggest going to www.gamedev.net and reading their tutorials on AI.
Ok thanks.
Heres a Tic-Tac-Toe game I made with A.I. awile ago, dodnt know if it will help though.

// Tic-Tac-Toe
// Plays the game of tic-tac-toe against a human opponent
// Uses pointers instead of refernces for function parameters
//By blindsiner

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

// global constants
const char X = 'X';
const char O = 'O';
const char EMPTY = ' ';
const char TIE = 'T';
const char NO_ONE = 'N';

// function prototypes
void instructions();
char askYesNo(string question);
int askNumber(string question, int high, int low = 0);
char humanPiece();
char opponent(char piece);
void displayBoard(const vector<char>* const pBoard);
char winner(const vector<char>* const pBoard);
bool isLegal(const vector<char>* const pBoard, int move);
int humanMove(const vector<char>* const pBoard, char human);
int computerMove(vector<char> board, char computer);
void announceWinner(char winner, char computer, char human);

// main function
int main()
{
int move;
const int NUM_SQUARES = 9;
vector<char> board(NUM_SQUARES, EMPTY);

instructions();
char human = humanPiece();
char computer = opponent(human);
char turn = X;
displayBoard(&board);

while (winner(&board) == NO_ONE)
{
if (turn == human)
{
move = humanMove(&board, human);
board[move] = human;
}
else
{
move = computerMove(board, computer);
board[move] = computer;
}
displayBoard(&board);
turn = opponent(turn);
}

announceWinner(winner(&board), computer, human);

return 0;
}

// functions
void instructions()
{
cout << "Welcome to the ultimate man-machine showdown: Tic-Tac-Toe.\n";
cout << "--where human brain is pit against silicon processor\n\n";

cout << "Make your move known by entering a number, 0 - 8. The number\n";
cout << "corresponds to the desired board position, as illustrated:\n\n";

cout << " 0 | 1 | 2\n";
cout << " ---------\n";
cout << " 3 | 4 | 5\n";
cout << " ---------\n";
cout << " 6 | 7 | 8\n\n";

cout << "Prepare yourself, human. The battle is about to begin.\n\n";
}

char askYesNo(string question)
{
char response;
do
{
cout << question << " (y/n): ";
cin >> response;
} while (response != 'y' && response != 'n');

return response;
}

int askNumber(string question, int high, int low)
{
int number;
do
{
cout << question << " (" << low << " - " << high << "): ";
cin >> number;
} while (number > high || number < low);

return number;
}

char humanPiece()
{
char go_first = askYesNo("Do you require the first move?");
if (go_first == 'y')
{
cout << "\nThen take the first move. You will need it.\n";
return X;
}
else
{
cout << "\nYour bravery will be your undoing... I will go first.\n";
return O;
}
}

char opponent(char piece)
{
if (piece == X)
return O;
else
return X;
}

void displayBoard(const vector<char>* const pBoard)
{
cout << "\n\t" << (*pBoard)[0] << " | " << (*pBoard)[1] << " | " << (*pBoard)[2];
cout << "\n\t" << "---------";
cout << "\n\t" << (*pBoard)[3] << " | " << (*pBoard)[4] << " | " << (*pBoard)[5];
cout << "\n\t" << "---------";
cout << "\n\t" << (*pBoard)[6] << " | " << (*pBoard)[7] << " | " << (*pBoard)[8];
cout << "\n\n";
}

char winner(const vector<char>* const pBoard)
{
// all possible winning rows
const int WINNING_ROWS[8][3] = { {0, 1, 2},
{3, 4, 5},
{6, 7, 8},
{0, 3, 6},
{1, 4, 7},
{2, 5, 8},
{0, 4, 8},
{2, 4, 6} };
const int TOTAL_ROWS = 8;

// if any winning row has three values that are the same (and not EMPTY),
// then we have a winner
for(int row = 0; row < TOTAL_ROWS; ++row)
{
if ( ((*pBoard)[WINNING_ROWS[row][0]] != EMPTY) &&
((*pBoard)[WINNING_ROWS[row][0]] == (*pBoard)[WINNING_ROWS[row][1]]) &&
((*pBoard)[WINNING_ROWS[row][1]] == (*pBoard)[WINNING_ROWS[row][2]]) )
{
return (*pBoard)[WINNING_ROWS[row][0]];
}
}

// since nobody has won, check for a tie (no empty squares left)
if (count(pBoard->begin(), pBoard->end(), EMPTY) == 0)
return TIE;

// since nobody has won and it isn't a tie, the game ain't over
return NO_ONE;
}

inline bool isLegal(int move, const vector<char>* pBoard)
{
return ((*pBoard)[move] == EMPTY);
}

int humanMove(const vector<char>* const pBoard, char human)
{
int move = askNumber("Where will you move?", (pBoard->size() - 1));
while (!isLegal(move, pBoard))
{
cout << "\nThat square is already occupied, foolish human.\n";
move = askNumber("Where will you move?", (pBoard->size() - 1));
}
cout << "Fine...\n";
return move;
}

int computerMove(vector<char> board, char computer)
{
cout << "I shall take square number ";

// if computer can win on next move, make that move
for(int move = 0; move < board.size(); ++move)
{
if (isLegal(move, &board))
{
board[move] = computer;
if (winner(&board) == computer)
{
cout << move << endl;
return move;
}
// done checking this move, undo it
board[move] = EMPTY;
}
}

// if human can win on next move, block that move
char human = opponent(computer);
for(int move = 0; move < board.size(); ++move)
{
if (isLegal(move, &board))
{
board[move] = human;
if (winner(&board) == human)
{
cout << move << endl;
return move;
}
// done checking this move, undo it
board[move] = EMPTY;
}
}

// the best moves to make, in order
const int BEST_MOVES[] = {4, 0, 2, 6, 8, 1, 3, 5, 7};
// since no one can win on next move, pick best open square
for(int i = 0; i < board.size(); ++i)
{
int move = BEST_MOVES[i];
if (isLegal(move, &board))
{
cout << move << endl;
return move;
}
}
}

void announceWinner(char winner, char computer, char human)
{
if (winner == computer)
{
cout << winner << "'s won!\n";
cout << "As I predicted, human, I am triumphant once more -- proof\n";
cout << "that computers are superior to humans in all regards.\n";
}

else if (winner == human)
{
cout << winner << "'s won!\n";
cout << "No, no! It cannot be! Somehow you tricked me, human.\n";
cout << "But never again! I, the computer, so swear it!\n";
}

else
{
cout << "It's a tie.\n";
cout << "You were most lucky, human, and somehow managed to tie me.\n";
cout << "Celebrate... for this is the best you will ever achieve.\n";
}
}

Topic archived. No new replies allowed.