Need help with tic-tac-toe

Alright, I have quite some trouble here, can't come up with anything. I need code that would check if there is already a symbol placed in the specified cell, and if yes, prompt to input correct coordinates. Here's the code:
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
#include <iostream>
#include <conio.h>

using namespace std;


void rysuj_plansze(),podaj_xy(),sprawdzenie();
char plansza[3][3];
char gracz_X, gracz_O;
int over=1,menu=1,game_var=0;
int player_var=1;

int main()
{
	do
	{

		cout<<"1 - Rozpocznij gre"<<endl;
		cout<<"2 - Wyjdz z gry"<<endl<<endl;
		cout<<"> ";
		cin>>menu;

		switch (menu)
		{
		case 1:
			{
				do
				{
					system("cls");
					podaj_xy();
					sprawdzenie();
					game_var++;
					player_var++;
					
				} while (game_var<9);
				game_var=0;
				player_var=1;
				memset (plansza, NULL, 9);
				system("cls");
			} break;
		
		case 2: 
			{
				over = 0;
			} break;
		}
	}while(over!=0);

		return 0;
}

void sprawdzenie()
{
	if ((plansza[0][0]==plansza[1][1]) && (plansza[1][1]==plansza[2][2]) && (plansza[0][0]!=NULL)) game_var=9;

	if ((plansza[0][0]==plansza[0][1]) && (plansza[0][1]==plansza[0][2]) && (plansza[0][0]!=NULL)) game_var=9;

	if ((plansza[0][0]==plansza[1][0]) && (plansza[1][0]==plansza[2][0]) && (plansza[0][0]!=NULL)) game_var=9;

	if ((plansza[0][1]==plansza[1][1]) && (plansza[1][1]==plansza[2][1]) && (plansza[0][1]!=NULL)) game_var=9;

	if ((plansza[0][2]==plansza[1][2]) && (plansza[1][1]==plansza[2][2]) && (plansza[0][2]!=NULL)) game_var=9;

	if ((plansza[1][0]==plansza[1][1]) && (plansza[1][1]==plansza[2][2]) && (plansza[1][0]!=NULL)) game_var=9;

	if ((plansza[2][0]==plansza[2][1]) && (plansza[2][1]==plansza[2][2]) && (plansza[2][0]!=NULL)) game_var=9;

	if ((plansza[2][0]==plansza[1][1]) && (plansza[2][1]==plansza[0][2]) && (plansza[2][0]!=NULL)) game_var=9;
}

void podaj_xy()
{
	int x=1,y=1;
	do
	{
		rysuj_plansze();
		        if ((x<1)||(x>4)||(y<1)||(y>4)) 
			{
				cout<<"Nieprawidlowa wartosc.";
				_sleep(2000);
				rysuj_plansze();
			}

		if (player_var%2==1)
			cout<<"Teraz ruch wykonuje X"<<endl; 
		else 
			cout<<"Teraz ruch wykonuje O"<<endl;

		cout<<"Podaj kolumne i wiersz ruchu : "<<endl;
		cout<<"Kolumna: ";
		cin>>x;
		cout<<"Wiersz: ";
		cin>>y;
	} while ((x<1)||(x>4)||(y<1)||(y>4));
	if (player_var%2==1) plansza[y-1][x-1]='X'; else plansza[y-1][x-1]='O';
}

void rysuj_plansze()
{
	system("cls");
	for (int i = 0; i < 3; i++)
	{
		for (int j = 0; j < 3; j++)
		{
			cout << plansza[i][j];
			if (j < 2) cout << " | ";
		}
		cout << endl;
		if (i < 2) cout << "- - - - -" << endl;	
	}
}
Last edited on
Not an elegant solution, but it works:
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
void podaj_xy()
{
	int x=1,y=1;
	do
	{
		rysuj_plansze();
		        if ((x<1)||(x>4)||(y<1)||(y>4)) 
			{
				cout<<"Nieprawidlowa wartosc.";
				_sleep(2000);
				rysuj_plansze();
			}

		if (player_var%2==1)
			cout<<"Teraz ruch wykonuje X"<<endl; 
		else 
			cout<<"Teraz ruch wykonuje O"<<endl;

		cout<<"Podaj kolumne i wiersz ruchu : "<<endl;
		cout<<"Kolumna: ";
		cin>>x;
		cout<<"Wiersz: ";
		cin>>y;

		if(plansza[y-1][x-1] == 'X' || plansza[y-1][x-1] == 'O')
		{
			cout<<"This cell is already filled. Please press any key."<<endl;
			getch();
		}

	} while (((x<1)||(x>4)||(y<1)||(y>4)) || (plansza[y-1][x-1] == 'X' || plansza[y-1][x-1] == 'O'));
		
	if (player_var%2==1) plansza[y-1][x-1]='X'; else plansza[y-1][x-1]='O';
}


A duplicate problem...
See http://www.cplusplus.com/forum/beginner/88486/
Thanks kameswarib, that's perfect.
Topic archived. No new replies allowed.