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
|
//This is a program that will aloow the user to play a game of tic-tac-toe
//Ahmad Abu-Spetani CS 121 Program 5 4/21/17
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>
void InitaializeBoard (char board [][3]);
void DisplayBoard (char board [][3]);
void GetBoardConfiguration (char board [][3]);
bool check_winner (char board[3][3], char who);
using namespace std;
struct Combo
{ int row;
int col;
};
int main()
{
char play;
char gameBoard[3][3];
InitaializeBoard(gameBoard);
int totalGames;
cout << showpoint;
cout << fixed;
cout << setprecision (2);
float xpercentage;
float opercentage;
float drawpercentage;
cout << "Would you like to play the game? Enter 'y' for yes and 'n' for no." << endl;
cin >> play;
while (play == 'y')
{
GetBoardConfiguration(gameBoard);
DisplayBoard(gameBoard);
int x;
int o;
bool win1 = check_winner(gameBoard, x); //trying to figure out if x won or o won, not working
bool win2 = check_winner(gameBoard, o);
int xWin = 0;
int oWin = 0;
int draw = 0;
if(win1 = true)
{
cout << "Player x won" << endl;
xWin++;
}
else(win2 = true);
{
cout<< "Player o won" <<endl;
oWin++;
}
if(win1 = false && win2 = false) // error under false
{
cout << "It is a draw" << endl;
draw++;
}
totalGames = xWin + oWin + draw;
xpercentage = xWin / (double) totalGames;
opercentage = oWin / (double) totalGames;
drawpercentage = draw / (double) totalGames;
cout << "Would you like to play the game? Enter 'y' for yes and 'n' for no." << endl;
cin >> play;
}
cout << "The total number of games played was " << totalGames << endl;
cout << "Player x's winning percentage: " << xpercentage << endl;
cout << "Player x's losing percentage: " << opercentage << endl;
cout << "Player o's winning percentage: " << opercentage << endl;
cout << "Player o's losing percentage: " << xpercentage << endl;
cout << "Draw percentage: " << drawpercentage << endl;
system("pause");
return 0;
}
//******************************************************************************
void InitaializeBoard (char board [][3])
{
for(int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
board[i][j] = ' ';
}
}
return;
}
//******************************************************************************
void DisplayBoard (char board [][3])
{
//Displays game configuration
for (int row = 0; row < 3; row++)
{
for (int col = 0; col < 3; col++)
cout << board[row][col] << " ";
cout << endl;
}
return;
}
//******************************************************************************
void GetBoardConfiguration (char board [][3])
{
char extra; //to get rid of extra characters in input stream
cout << "Enter 9 characters; either blank, x, or o, and then press enter" << endl;
cin.get(extra);
//reads in game configuration
for (int row = 0; row < 3; row++)
{
for (int col = 0; col < 3; col++)
{
cin.get(board[row][col]);
}
}
return;
}
//*****************************************************************************
bool check_winner (char board[3][3], char who)
{ const Combo combo[8][3] =
{ 0,0, 0,1, 0,2, // Row 0
1,0, 1,1, 1,2, // Row 1
2,0, 2,1, 2,2, // Row 2
0,0, 1,0, 2,0, // Col 0
1,0, 1,1, 2,1, // Col 1
2,0, 2,1, 2,2, // Col 2
0,0, 1,1, 2,2, // LR Diagonal
0,2, 1,1, 2,0, // RL Diagonal
};
for (int i=0; i<8; i++) // Check each combo
{ int count = 0;
for (int j=0; j<3; j++) // Check row and col
{ if (board[combo[i][j].row][combo[i][j].col] == who)
count++; // One cell matched
}
if (count == 3)
return true; // All three cells matched
}
return false; // No combo's matched
}
//*****************************************************************************
|