Tic Tac Toe code

Solved
Last edited on
So if I insert (1,2) it would check row 1, column 2 and return the value 5

1D arrays only have one row. You need to use a 2D array in order for that to work. This should illustrate the difference a little better:
1
2
3
4
orig[9] = {0, 1, 2, 3, 4, 5, 6, 7, 8}; // Row 0
orig[3][3] = {{0, 1, 2},  // Row 0
	      {3, 4, 5},  // Row 1
	      {6, 7, 8}}; // Row 2 
Solved
Last edited on
Solved
Last edited on
@helloworld135

For the 1D array, you could go like this..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int index(int i, int j)
{
const int orig[9]={0,1,2,3,4,5,6,7,8};
 // for (i = 0; i < 2; i++)// These should actually be for (i = 0; i < 3; i++) 
  // {
  //      for (j = 0; j < 2; j++)// and for (j = 0; j < 3; j++)
        //{
         // }
// Above not needed

i*=3+j;// Taking i and multiply by 3, then add j
// If i = 1, then i=3. Add 2, ( which is j ), and you get 5
// If i = 0, then i=0. Add 2, ( which is j ), and you get 2
// and same for line 2.  If i = 2, then i=6. Add 2, ( which is j ), and you get 8

cout << "The number " << orig[i] << " is at that location.."<<endl;
 // }
}
Last edited on
Solved
Last edited on
@helloworld135

You have to pass the array, or arrays, to the functions, with the correct names. I'm not quite sure why you need a copy, but here it is. Also, this was wrong.
ans=i+2*j;

It is supposed to be..
ans=i*3+j;

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
#include <iostream>

const int X = 1;
const int O = -1;
const int None = 0;

int index(int board[9], int i, int j)// Pass the array to the function
{
int ans=i*3+j;
std::cout << "The number " << board[ans] << " is at that location.."<<std::endl;
std::cout << "Put a player in the location" << std::endl;
board[ans]=O;
return  board[ans];
}

void copyBoard(const int board[9], int copy[9])// Again, pass the array, plus the second one
{
	for (int i=0;i<9;i++)
	copy[i]=board[i]; 
}

/*
 * @return X if the X side has won
 *         O if the O side has won
 *         None if neither X nor O has won
 */
int checkBoard(int board[9]){

   if (board[0]==board[1]==board[2]=='1')
   std::cout<< "Player 1 wins";
	
	else if (board[3]==board[4]==board[5]=='1')
	   std::cout<< "Player 1 wins";
	else if (board[6]==board[7]==board[8]=='1')
   std::cout<< "Player 1 wins";
	
	else
		std::cout << "Nobody has won :(.";  
}
  


int main(){
int board[9]={0,1,2,3,4,5,6,7,8};
int copy[9];
std::cout<<index(board, 2,1)<<" ";
copyBoard(board, copy);
for (int i=0;i<9;i++)
  std::cout << copy[i] << " ";
 
 return 0;
 }
Solved
Last edited on
@@helloworld135

why did you say put a player in that location, and set board[ans] to zero?


I only put a player in that position, so that you can that the copy array, truly had a copy of the orig board. Plus, that was not a zero I put there, but the letter O, which has the value of -1

Everything in your program seems to work as you intend. Carry on.
Solved
Last edited on
Your board array takes integers, but your checking the contents for a char. Try removing the single quotes around the '-1' and '1', and see if that helps.

I also don't think if (board[0]==board[1]==board[2]=='1') type of coding works. You'll have to do it this way..

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
int checkBoard(const int board[9]){

  if (board[0]== 1 && board[1]== 1 && board[2]== 1)
   return X;
else if (board[3]== 1 && board[4]== 1 && board[5]== 1)
	return X;
else if (board[6]== 1 && board[7]== 1 && board[8]== 1)
	return X;
else if (board[0]== 1 && board[3]== 1 && board[6]== 1)
		return X;
else if (board[1]== 1 && board[4]== 1 && board[7]== 1)
	return X;
else if (board[2]== 1 && board[5]== 1 && board[8]== 1)
		return X;
else if (board[0]== 1 && board[4]== 1 && board[8]== 1)
		return X;
else if (board[2]== 1 && board[4]== 1 && board[6]== 1)
		return X;
  if (board[0]== -1 && board[1]== -1 && board[2]== -1)
   return O;
else if (board[3]== -1 && board[4]== -1 && board[5]== -1)
	return O;
else if (board[6]== -1 && board[7]== -1 && board[8]== -1)
	return O;
else if (board[0]== -1 && board[3]== -1 && board[6]== -1)
		return O;
else if (board[1]== -1 && board[4]== -1 && board[7]== -1)
	return O;
else if (board[2]== -1 && board[5]== -1 && board[8]== -1)
		return O;
else if (board[0]== -1 && board[4]== -1 && board[8]== -1)
		return O;
else if (board[2]== -1 && board[4]== -1 && board[6]== -1)
		return O;
else
	return None;		
}
int main(){
  int win; // Added new variable to check for win or draw
  win = checkBoard(board);
  if(win == -1)
    std::cout << "O is the winner" << std::endl;
  else if(win == 1)
    std::cout << "X is the winner" << std::endl;
  else
    std::cout << "It's a DRAW!" << std::endl; 	
 
 return 0;
 }
Last edited on
Thanks a lot

Is there any point in editing my code to say if X and O are a winner?(I know this isn't possible in a real game.) Right now if I set all my values to 1/-1 sometimes it will say X or O is a winner, even if both are(by my array.)
The reason it says X is the winner, when there is two or more winning combos in the array, is because the return in the function is used on the first instance it finds in the if/else if, checks. If you were playing the game by placing markers, then checking for win after each move, then there can be only one winning combo. So, in a real game, X or O, can be declared a winner.

Lastly, it's considered poor conduct to remove all your posts. Others can benefit from it, as it can help them through similar problems. So, here is the final coding that was done.

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
#include <iostream>
const int board[9]={-1,1,-1, -1, -1, 1, 1,-1, 1};
const int orig[9]={2,3,4,5,1,2,3,4,5};
const int test[9]={1,1,1,4,3,3,3,3,1};
const int X = 1;
const int O = -1;
const int None = 0;

int index(int i, int j)// Pass the array to the function
{
int ans=i*3+j;
std::cout << "The number " << board[ans] << " is at that location.."<<std::endl;
return  board[ans];
}

void copyBoard(const int orig[9], int copy[9])// Again, pass the array, plus the second one
{
	for (int i=0;i<9;i++)
	copy[i]=orig[i]; 
}

/*
 * @return X if the X side has won
 *         O if the O side has won
 *         None if neither X nor O has won
 */
int checkBoard(const int board[9]){

  if (board[0]== 1 && board[1]== 1 && board[2]== 1)
   return X;
else if (board[3]== 1 && board[4]== 1 && board[5]== 1)
	return X;
else if (board[6]== 1 && board[7]== 1 && board[8]== 1)
	return X;
else if (board[0]== 1 && board[3]== 1 && board[6]== 1)
		return X;
else if (board[1]== 1 && board[4]== 1 && board[7]== 1)
	return X;
else if (board[2]== 1 && board[5]== 1 && board[8]== 1)
		return X;
else if (board[0]== 1 && board[4]== 1 && board[8]== 1)
		return X;
else if (board[2]== 1 && board[4]== 1 && board[6]== 1)
		return X;
  if (board[0]== -1 && board[1]== -1 && board[2]== -1)
   return O;
else if (board[3]== -1 && board[4]== -1 && board[5]== -1)
	return O;
else if (board[6]== -1 && board[7]== -1 && board[8]== -1)
	return O;
else if (board[0]== -1 && board[3]== -1 && board[6]== -1)
		return O;
else if (board[1]== -1 && board[4]== -1 && board[7]== -1)
	return O;
else if (board[2]== -1 && board[5]== -1 && board[8]== -1)
		return O;
else if (board[0]== -1 && board[4]== -1 && board[8]== -1)
		return O;
else if (board[2]== -1 && board[4]== -1 && board[6]== -1)
		return O;
else
	return None;		
}
int main(){
  int win = checkBoard(board);
  if(win == -1)
    std::cout << "O is the winner" << std::endl;
  else if(win == 1)
    std::cout << "X is the winner" << std::endl;
  else
    std::cout << "It's a DRAW!" << std::endl; 	
 
 return 0;
 }
Last edited on
Topic archived. No new replies allowed.