-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
186 lines (184 loc) · 4.8 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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include<iostream>
#include<cstdlib>
#include<time.h>
using namespace std;
int main()
{
srand(time(0));
char option,marker1,marker2;
const int size = 100;
char firstPlayer[size], secondPlayer[size];
do {
cout << "Enter name of first player: ";
cin.getline(firstPlayer, size);
cout << "Enter name of second player: ";
cin.getline(secondPlayer, size);
bool flag = rand() % 2;//randomly selecting person for toss
if (flag)
cout << firstPlayer;
else
cout << secondPlayer;
cout << " please select head(H) or tail(T) for toss: ";
cin >> option;
while (option != 'H' && option != 'h' && option != 'T' && option != 't') {
cout << "Wrong option entered! Please choose correct option: ";
cin >> option;//taking value again if wrong input entered
}
if (option == 'H' || option == 'h')//this is for checking either a person win toss or not
option = '1';
else
option = '0';
bool temp = rand() % 2;//toss happening here
cout << "Coin tosses for ";
if (temp == 0)
cout << "head" << endl;
else
cout << "tail" << endl;
if (int(option) == int(temp)) {//checking toss results
if (flag)
temp = 1;
else
temp = 0;
}
else {
if (flag)
temp = 0;
else
temp = 1;
}
if (temp) {
cout <<firstPlayer<< " won the toss!" << endl;
cout << "Please select your marker [o] or [x]: ";
cin >> marker1;
while (marker1 != 'x' && marker1 != 'X' && marker1 != '0' && marker1 != 'o' && marker1 != 'O') {
cout << "Wrong marker selected! Enter again: ";
cin >> marker1;
}
if (marker1 == 'x' || marker1 == 'X') {
marker2 = 'o';
}
else
marker2 = 'x';
}
else {
cout << secondPlayer << " won the toss!" << endl;
cout << "Please select your marker [o] or [x]: ";
cin >> marker2;
while (marker2 != 'x' && marker2 != 'X' && marker2 != '0' && marker2 != 'o' && marker2 != 'O') {
cout << "Wrong marker selected! Enter again: ";
cin >> marker2;
}
if (marker2 == 'x' || marker2 == 'X') {
marker1 = 'o';
}
else
marker1 = 'x';
}
char arr[10] = "123456789";
int number;
for (int i = 1, n = 0; i <= 9; i++) {//tic tac toe pattern
for (int j = 1; j < 18; j++)
{
if (j % 6 == 0)
cout << "|";
else if (i % 3 == 0 && i < 9)
cout << "_";
else if ((i == 2 || i == 5 || i == 8) && j % 3 == 0)
cout << arr[n++];
else
cout << " ";
}
cout << endl;
}
do {
flag = true;
if (temp) {
cout << firstPlayer << " - [" << marker1 << "] your turn: ";
do {
cin >> number;
for (int i = 0; i < 9; i++) {
if ((int(arr[i] - 48)) == number) {
flag = false;
break;
}
}
if (flag)
cout << "Wrong input! Please select from options: ";
} while (flag == true);
arr[number - 1] = marker1;
temp=temp-1;
}
else {
cout << secondPlayer << " - [" << marker2 << "] your turn: ";
do {
cin >> number;
for (int i = 0; i < 9; i++) {
if (int(arr[i] - 48) == number) {
flag = false;
break;
}
}
if (flag)
cout << "Wrong input! Please select from options: ";
} while (flag == true);
arr[number - 1] = marker2;
temp++;
}
if (arr[0] == arr[1] && arr[1] == arr[2])
flag = true;
else if (arr[3] == arr[4] && arr[4] == arr[5])
flag = true;
else if (arr[6] == arr[7] && arr[7] == arr[8])
flag = true;
else if (arr[0] == arr[3] && arr[3] == arr[6])
flag = true;
else if (arr[1] == arr[4] && arr[4] == arr[7])
flag = true;
else if (arr[2] == arr[5] && arr[5] == arr[8])
flag = true;
else if (arr[0] == arr[4] && arr[4] == arr[8])
flag = true;
else if (arr[2] == arr[4] && arr[4] == arr[6])
flag = true;
for (int i = 1, n = 0; i <= 9; i++) {
for (int j = 1; j < 18; j++)
{
if (j % 6 == 0)
cout << "|";
else if (i % 3 == 0 && i < 9)
cout << "_";
else if ((i == 2 || i == 5 || i == 8) && j % 3 == 0)
cout << arr[n++];
else
cout << " ";
}
cout << endl;
}
if (flag) {
if (temp)
cout << secondPlayer;
else
cout << firstPlayer;
cout << " Win the game." << endl;
break;
}
for (int i = 0; i < 9; i++) {
if (arr[i] >= '1' && arr[i] <= '9') {
flag = true;
}
}
} while (flag);
if (flag == false)
cout << "Match drawn" << endl;
cout << "Want to play again Y/N: ";
cin >> option;
while (option != 'Y' && option != 'y' && option != 'n' && option != 'N') {
cout << "Wrong input! Enter again: ";
cin >> option;
}
cin.ignore(); /* if user wants to play again newline character will
not remove from input stream (screen) created due to cin >> option if
this line is not entered while choosing yes it will not ask for first player
name because it directly recieves a newline with is terminator for getline*/
} while (option=='y'||option=='Y');
}