This repository was archived by the owner on Jan 1, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
136 lines (112 loc) · 3.56 KB
/
Copy pathmain.cpp
File metadata and controls
136 lines (112 loc) · 3.56 KB
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
/*
* Guillaume Dunant - Tic Tac Toe
* Laboratoire 1 - Prg 1
* 29.09.2022
*/
#include <iostream>
bool checkWin(char board[9], char plyChar) {
//Check the lines
for (int i = 0; i < 9; i+=3) {
if (board[i] == board[i + 1] && board[i] == board[i + 2] && board[i] == plyChar) {
return true;
}
}
//Check the columns
for (int i = 0; i < 3; i ++) {
if (board[i] == board[i + 3] && board[i] == board[i + 6] && board[i] == plyChar) {
return true;
}
}
//Check diagonals
if (board[0] == board[4] && board[0] == board[8] && board[0] == plyChar) {
return true;
} else if (board[2] == board[4] && board[2] == board[6] && board[2] == plyChar) {
return true;
}
//If nothing match
return false;
}
void displayBoard(char board[]){
std::cout << board[0] << " | " << board[1] << " | " << board[2] << std::endl;
std::cout << "---------" << std::endl;
std::cout << board[3] << " | " << board[4] << " | " << board[5] << std::endl;
std::cout << "---------" << std::endl;
std::cout << board[6] << " | " << board[7] << " | " << board[8] << std::endl;
/*
for(int i = 0; i < 9; i+=3)
{
std::cout << "\t" << board[i] << "\t" << board[i+1] << "\t" << board[i+2] << std::endl;
}*/
}
int convertInputToInt(char input)
{
//Check if the input is a number between 1 and 8
if (input >= 48 && input <= 56){
return int(input - 48);
}
//Return -1 if not a number
return -1;
}
bool isCaseEmpty(char board[], int number)
{
return (board[number] == ' ');
}
int main() {
//Variables
char boardChar[9] = {' ',' ',' ',' ',' ',' ',' ',' ',' '};
char actualPlayerChar = 'O';
char ply1Char = 'X';
char ply2Char = 'O';
bool notValidInput;
//Main code
do {
//Change the actual player
if(actualPlayerChar == ply1Char){
actualPlayerChar = ply2Char;
} else{
actualPlayerChar = ply1Char;
}
//Set not valid input to true
notValidInput = true;
do {
//Ask the player to play
if (actualPlayerChar == ply1Char) {
std::cout << "C'est au joueur 1 de jouer : " << std::endl;
} else {
std::cout << "C'est au joueur 2 de jouer : " << std::endl;
}
//Display the board
displayBoard(boardChar);
//Wait for the user input
char input;
std::cin >> input;
//Convert the input into int
int intInput = convertInputToInt(input);
//if the input is a number
if (intInput >= 0) {
//If the case is empty
if(isCaseEmpty(boardChar, intInput)) {
//Change the value of the board
boardChar[intInput] = actualPlayerChar;
notValidInput = false;
} else{
std::cout << "!!!! Case deja occupee !!!!" << std::endl;
}
}
//if not
else{
std::cout << "!!!! Entree invalide !!!!" << std::endl;
}
} while (notValidInput);
}while(!checkWin(boardChar, actualPlayerChar)); //Repeat until someone won
//Display the winner
if(actualPlayerChar == ply1Char) {
std::cout << "Player 1 a gagne!!!!!!" << std::endl;
}
else{
std::cout << "Player 2 a gagne!!!!!!" << std::endl;
}
//Display the board to show the final board
displayBoard(boardChar);
return 0;
}