-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
67 lines (59 loc) · 1.58 KB
/
main.cpp
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
#include <iostream>
#include "helper.h"
using namespace std;
int main()
{
// declaring board grid for the game
char board[boardSize];
for (int i = 0; i < boardSize; i++)
{
board[i] = '-';
}
int length = sizeof(board) / sizeof(board[0]);
show(board, length);
int tempEntry;
cout << endl;
while (true)
{
cout << "Enter a value from 1-9: ";
cin >> tempEntry;
// checking for user entry is valid or not
if ((!(tempEntry >= 1 && tempEntry <= boardSize)))
{
cout << "enter a valid entry!!!!" << endl;
continue;
}
// checking if the gride index is already filled or not
if (board[tempEntry - 1] != '-')
{
cout << "enter a valid entry!!!!" << endl;
continue;
}
tempEntry--;
board[tempEntry] = 'X';
// if satisfies the winner condtion then program will end
bool won = win(board, length);
show(board, length);
if (won == true)
break;
// generating random entry for computer player (for now its random)
int computerMove;
cout << endl
<< "Computer Move!" << endl;
while (true)
{
computerMove = (rand() % 9);
if (board[computerMove] == '-')
{
board[computerMove] = 'O';
break;
}
}
show(board, length);
// checking for winner
won = win(board, length);
if (won == true)
break;
}
return 0;
}