Tic-Tac-Toe

closed account (L1AkoG1T)
Hello everyone, I just started programming a few months ago and now I have decided to start a Tic-Tac-Toe program in C++ to test my skills of arrays and more. So far, everything has been working fine and the program has been compiling, however, there is one problem. Every time I enter 3,3 for the rows and columns, the program fills in the spaces 3,3 AND 2,3. When I input 2,3, the program does not fill any spaces at all. Could someone help me find out how to fix this issue? Thanks.

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
// Tic-Tac-Toe

#include "stdafx.h"
#include <iostream>

using namespace std;


char board [3] [3]= { { '-','-','-'} , {'-','-','-'} , {'-','-','-'} };

void DisplayBoard()
{
	//displays the recent playing board
	cout << "   1  2  3" << endl;
	cout << "1  " << board [0][0] << "  " << board [0][1] << "  " << board [0][2] << endl;
	cout << "2  " << board [1][0] << "  " << board [1][1] << "  " << board [2][2] << endl;
	cout << "3  " << board [2][0] << "  " << board [2][1] << "  " << board [2][2] << endl;
}

int main(int nNumberofArgs, char* pszArgs[])
{

	DisplayBoard();

	int row;
	int column;
	bool i;
	int row2;
	int column2;

	//---------------------------------------
	//--------------- Turns -----------------
	//---------------------------------------

	while (i == false)
	{
		//player 1s move

		cout << "Player one, enter your choice (row and column)";
		while(row >= 1 || row < 4)
		{
			cin >> row;

			if ((row>=4) || (row<1))
				cout<<endl<<"Invalid move. Try again with numbers from 1 to 3: ";
			else break;
		}

		while(column >= 1 || column < 4)
		{
			cin >> column;
			if ((column>=4) || (column<1))
				cout<<endl<<"Invalid move. Try again with numbers from 1 to 3: ";
			else break;
		}

		row--;
		column--;

		board [row] [column] = 'X';

		DisplayBoard();


		//player 2s move

		cout << "Player two, enter your choice (row and column)";
		while(row2 >= 1 || row2 < 4)
		{
			cin >> row2;

			if ((row2>=4) || (row2<1))
				cout<<endl<<"Invalid move. Try again with numbers from 1 to 3: ";
			else break;
		}

		while(column2 >= 1 || column2 < 4)
		{
			cin >> column2;
			if ((column2>=4) || (column2<1))
				cout<<endl<<"Invalid move. Try again with numbers from 1 to 3: ";
			else break;
		}

		row2--;
		column2--;

		board [row2] [column2] = 'O';

		DisplayBoard();


		//---------------------------------------
		//---------------------------------------
		//---------------------------------------
	}
	system("PAUSE");
	return 0;
}



The result for row: 3 column: 3:


   1  2  3
1  -  -  -
2  -  -  -
3  -  -  -
Player one, enter your choice (row and column)3
3
   1  2  3
1  -  -  -
2  -  -  X
3  -  -  X
Player two, enter your choice (row and column)



The result for row: 2 column: 3 :


   1  2  3
1  -  -  -
2  -  -  -
3  -  -  -
Player one, enter your choice (row and column)2
3
   1  2  3
1  -  -  -
2  -  -  -
3  -  -  -
Player two, enter your choice (row and column)
It's your DisplayBoard() function and the way it's printing the last column. It's printing [2][2] twice, rather than [1][2].

Also, for safety you should probably initialise i.
closed account (L1AkoG1T)
Oh wow, I can't believe I didn't see that. I will fix the i soon also. Thanks for helping.
On line 16, you print out board[2][2] instead of board[1][2].

You should also initialize your variables starting at line 25 to default values. Uninitialized variables get garbage values and could be anything at runtime. For example, line 35 is basically random - i could be either true or false.

EDIT: Beaten to the punch
Last edited on
Topic archived. No new replies allowed.