Created
August 7, 2015 16:55
-
-
Save ajnarayan/718d7fea9692f8442c20 to your computer and use it in GitHub Desktop.
A Simple tic-tac-toe C++ game
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
char square[9] = {'0','1','2','3','4','5','6','7','8'}; | |
int checkwin() | |
{ | |
if (square[0] == square [1] && square[1] == square[2] ) | |
{ if ( square [0] == 'X' ) | |
return 1; | |
else | |
return 2; | |
} | |
else | |
if (square[3] == square [4] && square[4] == square[5] ) | |
{ if ( square [3] == 'X' ) | |
return 1; | |
else | |
return 2; | |
} | |
else | |
if (square[6] == square [7] && square[7] == square[8] ) | |
{ if ( square [6] == 'X' ) | |
return 1; | |
else | |
return 2; | |
} | |
else | |
if (square[0] == square [3] && square[3] == square[6] ) | |
{ if ( square [0] == 'X' ) | |
return 1; | |
else | |
return 2; | |
} | |
else | |
if (square[1] == square [4] && square[4] == square[7] ) | |
{ if ( square [1] == 'X' ) | |
return 1; | |
else | |
return 2; | |
} | |
else | |
if (square[2] == square [5] && square[5] == square[8] ) | |
{ if ( square [2] == 'X' ) | |
return 1; | |
else | |
return 2; | |
} | |
else | |
if (square[0] == square [4] && square[4] == square[8] ) | |
{ if ( square [0] == 'X' ) | |
return 1; | |
else | |
return 2; | |
} | |
else | |
if (square[2] == square [4] && square[4] == square[6] ) | |
{ if ( square [2] == 'X' ) | |
return 1; | |
else | |
return 2; | |
} | |
else | |
if (square[0] == square [3] && square[3] == square[6] ) | |
{ if ( square [0] == 'X' ) | |
return 1; | |
else | |
return 2; | |
} | |
else | |
return 0; | |
} | |
void mark(int player, int box) | |
{ | |
if (player == 1 ) | |
{ | |
square[box] = 'X'; | |
} | |
else | |
square[box] = 'Y'; | |
} | |
void display() | |
{ | |
for(int i=0;i<9;i++) | |
{ | |
cout<< square[i] << "\t" ; | |
if (i == 2 || i== 5 || i==8) | |
cout<<"\n"; | |
} | |
} | |
int main() | |
{ | |
int player1 = 1, player2 =2 ; | |
int box, result = 0, flag = 0; | |
for(int i=1;i<5;i++) | |
{ | |
cout<< "\n Player " << player1 << "Enter the Box"; | |
cin>> box; | |
mark( player1, box); | |
display(); | |
result =checkwin(); | |
if (result == 1 ) | |
{ cout<<"\n Congratualtions! player " << player1 << " has Won "; | |
flag = 1; | |
break; | |
} | |
else | |
if (result == 2 ) | |
{ cout<<"\n Congratualtions! player " << player2 << " has Won "; | |
flag = 1; | |
break; | |
} | |
cout<< "\n Player " << player2 << "Enter the Box"; | |
cin>> box; | |
mark ( player2, box); | |
display(); | |
result =checkwin(); | |
if (result == 1 ) | |
{ cout<<"\n Congratualtions! player " << player1 << " has Won "; | |
flag = 1; | |
break; | |
} | |
else | |
if (result == 2 ) | |
{ cout<<"\n Congratualtions! player " << player2 << " has Won "; | |
flag = 1; | |
break; | |
} | |
} | |
if (flag == 0 ) | |
cout<<" \n Sorry, The game is a draw "; | |
return 0; | |
} |
Late to the party but here's my version. It's the best one in this thread.
The game is you (X) vs the computer (O) and is randomly decided who goes first. 50/50 chance.
It displays the game board with each position whether it's filled or not, and even tells you which positions are available.
There's unlimited retries and the board resets after every turn so there's no clutter.
You'll need to link a header file called random.hpp.
#include <iostream>
#include <vector> //to create vectors
#include <algorithm> // for .begin() and .end() for vectors
#include <cstdlib> //rand() and srand().
#include "random.hpp" //better at guessing random numbers
using Random = effolkronium::random_static; //from "random.hpp" file
bool checksSpaces(std::vector<int>ops, int choice){ //checks if the player enters a space that is not available.
if (std::find(ops.begin(), ops.end(), choice) != ops.end()) {
return true;}
else {
return false;}
}
void print(std::vector<int> const &ops){ //prints out available positions. found online.
for (auto const &i: ops) {
std::cout << i << " ";}
}
void board(std::vector<char>& vec){ //displays the game.
system("CLS");
std::cout << "\t\t Tic Tac Toe\n\n";
std::cout << "\t\t 7 8 9" <<std::endl;
std::cout << "\t\t | | " << std::endl;
std::cout << "\t\t " << vec[6]<<" | " << vec[7] <<" | " << vec[8] << std::endl;
std::cout << "\t\t _____|___|_____" << std::endl;
std::cout << "\t\t | | " << std::endl;
std::cout << "\t\t4 " << vec[3]<<" | " << vec[4] <<" | " << vec[5] << " 6 " << std::endl;
std::cout << "\t\t _____|___|_____" << std::endl;
std::cout << "\t\t | | " << std::endl;
std::cout << "\t\t " << vec[0]<<" | " << vec[1] <<" | " << vec[2] << std::endl;
std::cout << "\t\t | | " << std::endl;
std::cout << "\t\t 1 2 3";
}
void playerSelection(std::vector<int>& op, std::vector<char>& vec){ //The player choosing a spot
p_select:
std::cout << "\nPlease select an open space: ";
print(op);
std::cout << std::endl;
int player_selection{};
std::cin >> player_selection;
if (checksSpaces(op, player_selection)) {
vec[player_selection - 1] = 'X'; // -1 so the indices work out between 2 vectors
std::vector<int>::iterator itr = std::find(op.begin(), op.end(), player_selection);
int index = std::distance(op.begin(), itr);
op.erase(op.begin() + index);}
else if (!checksSpaces(op, player_selection)){
std::cout << "Invalid. Try again." << std::endl;
std::cin.clear(); //will erase the cin input
std::cin.ignore(10000, '\n');
goto p_select;}
}
void computerSelection(std::vector<int>& op, std::vector<char>& vec){ //computer randomly selecting a spot
srand(time(0)); // NEED THIS for random every time
int comp_select = std::rand() % (op.size());
int sel_elem = op[comp_select];
vec[sel_elem - 1] = 'O'; // -1 so the indices work out between 2 vectors
std::vector<int>::iterator itr = std::find(op.begin(), op.end(), sel_elem);
int index = std::distance(op.begin(), itr);
op.erase(op.begin() + index);
}
bool whoGoesFirst(){ // coin flip for who goes first
return (Random::get<bool>()); // true with 50% probability by default
}
bool aWinner(std::vector<char>vec) { //determines a winner
if (vec[0] == 'X' && vec[1] == 'X' && vec[2] == 'X' || vec[3] == 'X' && vec[4] == 'X' && vec[5] == 'X' || vec[6] == 'X' && vec[7] == 'X' && vec[8] == 'X') {
std::cout << "\nYou win! Horizontal." << std::endl;
return false;}
else if (vec[0] == 'X' && vec[3] == 'X' && vec[6] == 'X' || vec[1] == 'X' && vec[4] == 'X' && vec[7] == 'X' || vec[2] == 'X' && vec[5] == 'X' && vec[8] == 'X') {
std::cout << "\nYou win! Vertical." << std::endl;
return false;}
else if (vec[0] == 'X' && vec[4] == 'X' && vec[8] == 'X' || vec[2] == 'X' && vec[4] == 'X' && vec[6] == 'X') {
std::cout << "\nYou win! Diagonal." << std::endl;
return false;}
else if (vec[0] == 'O' && vec[1] == 'O' && vec[2] == 'O' || vec[3] == 'O' && vec[4] == 'O' && vec[5] == 'O' || vec[6] == 'O' && vec[7] == 'O' && vec[8] == 'O') {
std::cout << "\nComputer wins! Horizontal." << std::endl;
return false;}
else if (vec[0] == 'O' && vec[3] == 'O' && vec[6] == 'O' || vec[1] == 'O' && vec[4] == 'O' && vec[7] == 'O' || vec[2] == 'O' && vec[5] == 'O' && vec[8] == 'O') {
std::cout << "\nComputer wins! Vertical." << std::endl;
return false;}
else if (vec[0] == 'O' && vec[4] == 'O' && vec[8] == 'O' || vec[2] == 'O' && vec[4] == 'O' && vec[6] == 'O') {
std::cout << "\nComputer wins! Diagonal." << std::endl;
return false;}
else {
return true;}
}
int main(){
while(true){
std::vector<int> open_positions = {1,2,3,4,5,6,7,8,9}; //these two variables are passed by reference in functions to change them dynamically.
std::vector<char> pst = {' ',' ',' ',' ',' ',' ',' ',' ',' '};
board(pst);
switch (whoGoesFirst()){
case 0: //false. Player goes first
while (aWinner(pst) && open_positions.size() != 0){ //keeps the game going unless someone wins or no more spaces.
board(pst);
std::cout << "\nYou go first." << std::endl;
playerSelection(open_positions, pst);
if (aWinner(pst) && open_positions.size() != 0){ //Need this so the game stops at the moment someone wins
computerSelection(open_positions, pst);}
else {
break;}}
break;
case 1: //true. Computer goes first
while (aWinner(pst) && open_positions.size() != 0){
computerSelection(open_positions, pst);
board(pst);
std::cout << "\nComputer goes first." << std::endl;
if (aWinner(pst) && open_positions.size() != 0){
playerSelection(open_positions, pst);}
else {
break;}}
break;}
board(pst);
aWinner(pst);
if (open_positions.size() == 0){
std::cout << "\nNo one wins.";
}
std::cout << "\nPlay again? (y/n): ";
char retry{};
std::cin >> retry;
if (retry == 'y'){}
else {
break;}}
std::cout << "Thanks for playing!" << std::endl;
return 0;
}
#include <bits/stdc++.h>
using namespace std;
void Print(vector<vector<char>> &board)
{
for (int i = 0; i < 3; i++)
{
cout << "-------" << endl;
cout << "|" << board[i][0] << "|" << board[i][1] << "|" << board[i][2] << "|" << endl;
}
cout << "-------" << endl;
return;
}
bool win(vector<vector<char>> &board, int row, int col)
{
char chk = board[row - 1][col - 1];
// Check the diagonals
if (board[0][0] == chk && board[1][1] == chk && board[2][2] == chk)
return true;
if (board[0][2] == chk && board[1][1] == chk && board[2][0] == chk)
return true;
// Checking Rows and columns
for (int i = 0; i < 3; i++)
{
if (board[i][0] == chk && board[i][1] == chk && board[i][2] == chk)
return true;
if (board[0][i] == chk && board[1][i] == chk && board[2][i] == chk)
return true;
}
return false;
}
int main()
{
int count = 0;
vector<vector<char>> board(3, vector<char>(3, ' '));
bool gameon = true;
bool first = true, second = false;
while (gameon)
{
if (count == 9)
{
cout << "That's a Draw! Game Over!" << endl;
break;
}
if (first)
{
cout << "First Player Choose the Row and Column numbers" << endl;
}
else
{
cout << "Second Player Choose the Row and Column numbers" << endl;
}
int row, col;
cout << "Row Number (1,2,3) - ";
cin >> row;
cout << endl
<< "Column Number (1,2,3) - ";
cin >> col;
if (board[row - 1][col - 1] != ' ')
{
cout << "\n This cell is occupied, choose another" << endl;
continue;
}
if (first)
board[row - 1][col - 1] = 'X';
else
board[row - 1][col - 1] = 'O';
count++;
Print(board);
bool won = win(board, row, col);
if (won)
{
if (first)
cout << "First Player Won! Congratulations!";
else
cout << "Second Player Won! Congratulations!";
return 0;
gameon = false;
}
first = !first;
second = !second;
}
cout << "\n Game Over";
return 0;
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
/Here is my implementation/
#include < iostream >
using namespace std;
int isAnyoneWinner(int arr[]);
int findBestNextMove(int arr[]);
void DisplayBoard(int arr[]);
int main()
{
int array[9] = { 0 };
int player = 1;
int nextMove = 0;
cout << endl << "Who is first player to start? ( pls enter 1 for Computer[X], 2 for Human-Being[O]) = ";
cin >> player;
}
int findBestNextMove(int arr[])
{
int possibleWinningMoves[8][3] = { {0,4,8},{2,4,6},{0,3,6},{1,4,7},{2,5,8},{0,1,2},{3,4,5},{6,7,8} };
int Winner = 0;
int potentialWinningPos = -1;
}
// A function to show the current board status
void DisplayBoard(int arr[])
{
cout << endl << endl;
cout << "| " << (char)arr[0] << " | " << (char)arr[1] << " | " << (char)arr[2] << " |" << endl;
cout << "||" << endl;
cout << "| " << (char)arr[3] << " | " << (char)arr[4] << " | " << (char)arr[5] << " |" << endl;
cout << "||" << endl;
cout << "| " << (char)arr[6] << " | " << (char)arr[7] << " | " << (char)arr[8] << " |" << endl;
return;
}
int isAnyoneWinner(int arr[])
{
int WinningRowColumn[8];
int Winner = 0;
WinningRowColumn[0] = arr[0] + arr[3] + arr[6];
WinningRowColumn[1] = arr[1] + arr[4] + arr[7];
WinningRowColumn[2] = arr[2] + arr[5] + arr[8];
}