tic tac toe

Ok here is a situation.
"I have to create tic tac toe game.
The program provides a level playing field for two players , check the correctness of the march , followed by the situation on the field , and removes the winner.
The program play against people in every walk searching for the best variant of the current situation.
At the beginning you choose to offer - to play against people or against the computer . At the end of the game offers to play again. "

Besically i have to create game with menu where you chose to play with player 2 or PC.

i have some ideas how to do this by creating a field with numbers
123
456
789
and than when you enter number it changes to "x" or "o"...
also if i could create a field with lines it would be awesome like this or something like that:
1 | 2 | 3
----------
4 | 5 | 6
----------
7 | 8 | 9

I would appreciate any help you guys could provide ;)

What exactly do you need help with?

if i could create a field with lines it would be awesome like this or something like that:
Something like this?
http://puu.sh/hnPBp/3eba5e2f6f.png
yep something like that but i just thought that fields should be filed with something first so its easier to mark the right field... maybe i dont know maybe sounds crazi but is it possible to mark fields with numbers but hide them llike it works by entering number but on screen it apears as blank like
1 | 2 | 3
----------
4 | 5 | 6
----------
7 | 8 | 9

and appears as

| |
----------
| |
----------
| |
Last edited on
Yes. it is completely possible. Like that? http://puu.sh/hnWxd/b5273e6e72.png

Only difference is how you process input (I had to change 4 lines to make this change)
exactly :)
any other tips? :)
What do you need help with?

Showing board? Getting input? Checking victory condition? Implementing an AI?
ok i was able to make a game against player still need to figure out how to:
*make mennu where you can chose to play against player or PC
*after every game how to add "do you want to play again" and return to menu
*and make player 2 PC VI who will play with player :)



this is what im working with right now :)

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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include <iostream>
using namespace std;

char laukums[10] = {'o','1','2','3','4','5','6','7','8','9'};

int checkwin();
void galds();

int main()
{
	int player = 1,i,izvele;

	char mark;
	do
	{
		galds();
		player=(player%2)?1:2;

		cout << "Speeleetaaj " << player << ", ievadi laukuma numuru:  ";
		cin >> izvele;

		mark=(player == 1) ? 'X' : 'O';

		if (izvele == 1 && laukums[1] == '1')

			laukums[1] = mark;
		else if (izvele == 2 && laukums[2] == '2')

			laukums[2] = mark;
		else if (izvele == 3 && laukums[3] == '3')

			laukums[3] = mark;
		else if (izvele == 4 && laukums[4] == '4')

			laukums[4] = mark;
		else if (izvele == 5 && laukums[5] == '5')

			laukums[5] = mark;
		else if (izvele == 6 && laukums[6] == '6')

			laukums[6] = mark;
		else if (izvele == 7 && laukums[7] == '7')

			laukums[7] = mark;
		else if (izvele == 8 && laukums[8] == '8')

			laukums[8] = mark;
		else if (izvele == 9 && laukums[9] == '9')

			laukums[9] = mark;
		else
		{
			cout<<"Invalid move ";

			player--;
			cin.ignore();
			cin.get();
		}
		i=checkwin();

		player++;
	}while(i==-1);
	galds();
	if(i==1)

		cout<<"==>\aPlayer "<<--player<<" win ";
	else
		cout<<"==>\aGame draw";

	cin.ignore();
	cin.get();
	return 0;
}

/*** uzvareetaaja noteiceejs ******/

int checkwin()
{
	if (laukums[1] == laukums[2] && laukums[2] == laukums[3])

		return 1;
	else if (laukums[4] == laukums[5] && laukums[5] == laukums[6])

		return 1;
	else if (laukums[7] == laukums[8] && laukums[8] == laukums[9])

		return 1;
	else if (laukums[1] == laukums[4] && laukums[4] == laukums[7])

		return 1;
	else if (laukums[2] == laukums[5] && laukums[5] == laukums[8])

		return 1;
	else if (laukums[3] == laukums[6] && laukums[6] == laukums[9])

		return 1;
	else if (laukums[1] == laukums[5] && laukums[5] == laukums[9])

		return 1;
	else if (laukums[3] == laukums[5] && laukums[5] == laukums[7])

		return 1;
	else if (laukums[1] != '1' && laukums[2] != '2' && laukums[3] != '3' 
                    && laukums[4] != '4' && laukums[5] != '5' && laukums[6] != '6' 
                  && laukums[7] != '7' && laukums[8] != '8' && laukums[9] != '9')

		return 0;
	else
		return -1;
}


/**** galds ****/


void galds()
{
	system("cls");
	cout << "\n\n\Krustini un Nullites !\n\n";

	cout << "Speletajs 1 (X)  -  Speletajs 2 (O)" << endl << endl;
	cout << endl;

	cout << "     |     |     " << endl;
	cout << "  " << laukums[1] << "  |  " << laukums[2] << "  |  " << laukums[3] << endl;

	cout << "_____|_____|_____" << endl;
	cout << "     |     |     " << endl;

	cout << "  " << laukums[4] << "  |  " << laukums[5] << "  |  " << laukums[6] << endl;

	cout << "_____|_____|_____" << endl;
	cout << "     |     |     " << endl;

	cout << "  " << laukums[7] << "  |  " << laukums[8] << "  |  " << laukums[9] << endl;

	cout << "     |     |     " << endl << endl;
}


*make mennu where you can chose to play against player or PC
1) Move actual game outside of main.
2) Create menu like that:
1
2
3
4
5
6
7
8
9
10
int main()
{
    int choice;
    std::cout << "1. VS Human\n2. VS PC"
    std::cin >> choice;
    switch(choice) {
        case 1: play_vs_human(); break;
        case 2: play_vs_PC(); break;
    }
}
You will need to implement play_vs_* functions (or replace them with something equivalent)

*after every game how to add "do you want to play again" and return to menu
Wrap your menu selection in loop:
1
2
3
4
5
6
7
8
9
10
int main()
{
    std::string answer;
    do {
        //Menu from previous point

        std::cout << "Do you want to play again? (y/n)\n";
        std::cin >> answer
    } while(answer == "y");
}

*and make player 2 PC VI who will play with player :)
When it is PC turn, instead of asking for input, call function which checks board state and returns position. Implement AI in that function (good strategy to begin will be selecting random move from avaliable positions)
thanks @MiiNiPaa couldn't have done it without your help guess i have figured everything out and now i only have to add some tweaks but it works perfectly :)
Topic archived. No new replies allowed.