fl3kaucicpp/IksOks/iksoks.cpp

94 lines
2.0 KiB
C++
Raw Normal View History

2024-05-12 11:34:00 +00:00
#include <iostream>
using namespace std;
void drawBoard(char board[3][3]) {
cout << board[0][0] << " |" << board[0][1] << "| " << board[0][2] << endl;
cout << "-------\n";
cout << board[1][0] << " |" << board[1][1] << "| " << board[1][2] << endl;
cout << "-------\n";
cout << board[2][0] << " |" << board[2][1] << "| " << board[2][2] << endl;
}
2024-05-12 16:33:12 +00:00
bool checkWin(char board[3][3], char player) {
2024-05-12 11:34:00 +00:00
if(board[0][0] == player && board[1][1] == player && board[2][2] == player){
2024-05-12 16:33:12 +00:00
return true;}
2024-05-12 11:34:00 +00:00
if(board[2][0] == player && board[1][1] == player && board[0][2] == player){
2024-05-12 16:33:12 +00:00
return true;}
2024-05-12 12:49:34 +00:00
2024-05-12 16:33:12 +00:00
for(int i = 0; i < 3; i++){
if(board[i][0] == player && board[i][1] == player && board[i][2] == player){
return true;}
if(board[0][i] == player && board[1][i] == player && board[2][i] == player){
return true;}
}
return false;
2024-05-12 11:34:00 +00:00
}
int main(){
int row;
int col;
char player = 'X';
char board[3][3] = { { ' ', ' ', ' ' },
{ ' ', ' ', ' ' },
{ ' ', ' ', ' ' } };
cout << "Welcome to Tic-Tac-Toe!\n";
2024-05-12 16:33:12 +00:00
for(int i=0; i<9; i++){
2024-05-12 11:34:00 +00:00
drawBoard(board);
2024-05-12 11:56:22 +00:00
cout << "Player " << player << " choose a row (1-3): ";
cin >> row;
while(row > 3){
cout << "Invalid input\n";
cout << "Choose a row (1-3): \n";
cin >> row;
}
cout << "Choose a column (1-3): ";
cin >> col;
while(col > 3){
cout << "Invalid input\n";
cout << "Choose a column (1-3): \n";
cin >> col;
}
2024-05-12 11:34:00 +00:00
2024-05-12 12:23:29 +00:00
if(board[col-1][row-1] == ' '){
board[col-1][row-1] = player;
checkWin(board, player);
2024-05-12 16:33:12 +00:00
if(checkWin(board, player)){
break;
}
2024-05-12 12:23:29 +00:00
player = (player == 'X') ? 'O' : 'X';
}
else{
cout << "###Place is taken###\n";
i--;
continue;
}
2024-05-12 11:34:00 +00:00
}
2024-05-12 16:33:12 +00:00
if(checkWin(board, player))
cout << player << " WON!" << endl;
else
2024-05-12 12:49:34 +00:00
cout << "It's Draw\n";
2024-05-12 16:33:12 +00:00
drawBoard(board);
2024-05-12 12:23:29 +00:00
return 0;
2024-05-12 11:34:00 +00:00
}
2024-05-12 11:56:22 +00:00