-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.cpp
103 lines (92 loc) · 2.44 KB
/
Game.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
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
#include<iostream>
using namespace std;
#define capacity 100
#define Nop 2 //number of players
void init(char B[capacity][capacity], int& dim, char Pname[Nop][30], char Psym[], int& turn, int& wincount) {
cout << "Enter Diminesion...\n";
cin >> dim;
cout << "Enter Win count " << endl;
cin >> wincount;
//for user ..
for (int pi = 0; pi < Nop; pi++) {
cout << "Enter Player " << pi + 1 << ": ";
cin >> Pname[pi];
cout << "Enter Player " << pi + 1 << "symbol: ";
cin >> Psym[pi];
}
//for borard maiking
for (int ri = 0; ri < dim; ri++) {
for (int ci = 0; ci < dim; ci++) {
B[ri][ci] = '-';
}
}
turn = rand() % Nop;
}
void displayboard(char B[capacity][capacity], int dim) {
system("cls");
for (int ri = 0; ri < dim; ri++) {
for (int ci = 0; ci < dim; ci++) {
cout << B[ri][ci] << "\t";
}
cout << endl;
}
}
void playermsg(char pname[], char psym) {
cout << pname << "'s (" << psym << ") turn" << endl;
}
void playerturn(int &ri, int&ci) {
ri--;
ci--;
}
bool isvalid(char B[capacity][capacity], int dim, int ri, int ci) {
if (ri >= 0 && ri < dim && ci >= 0 && ci < dim && B[ri][ci] == '-')
return true;
else {
return false;
}
}
void updateboard(char B[capacity][capacity], int dim, int ri, int ci, char Psym) {
B[ri][ci] = Psym;
}
void turnchange(int& turn) {
turn = (turn + 1) % Nop;
}
int main()
{
char B[capacity][capacity]; //Board;
int dim; //dimension
char Pname[Nop][30];
char Psym[Nop];
int turn, ri, ci;
int wincount;
bool gameover = false;
init(B, dim, Pname, Psym, turn, wincount);
/* do {
displayboard(B, dim);
do {
playermsg(Pname[turn], Psym[turn]);
playerturn(ri, ci);
if (!isvalid(B, dim, ri, ci)) {
cout << "Invalid choice choose again...\n";
}
} while (!isvalid(B, dim, ri, ci));
updateboard(B, dim, ri, ci, Psym[turn]);
turnchange(turn);
} while (true);
*/
// Inside the game loop after displaying the board
do {
displayboard(B, dim);
do {
playermsg(Pname[turn], Psym[turn]);
cout << "Enter row and column (separated by space): ";
cin >> ri >> ci; // Take input for row and column indices here
playerturn(ri, ci);
if (!isvalid(B, dim, ri, ci)) {
cout << "Invalid choice choose again...\n";
}
} while (!isvalid(B, dim, ri, ci));
updateboard(B, dim, ri, ci, Psym[turn]);
turnchange(turn);
} while (true);
}