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
|
// HangMan.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include <iostream>
void location1(int *location, char table[]);
void location2(int *location, char table[]);
void drawTable (char table[]);
void gameRegulations(char table[], int turnCounter);
int main(int)
{
int continueGame = 1;
int locationHolder = 0;
char gameBoard[9] = {'1','2','3',
'4','5','6',
'7','8','9'};
while(continueGame==1){
std::cout << "=========================================" << std::endl;
std::cout << "=======Welcome to Tic Tac Toe============" << std::endl;
std::cout << "=========================================" << std::endl;
std::cout << "\n" << std::endl;
drawTable (gameBoard);
location1(&locationHolder, gameBoard);
std::cout << "Thanks for playing play again? 1- yes.....2-No" << std::endl;
std::cin >> continueGame;
}
return 0;
}
void location1(int *location, char table[]){
int number;
int turnCounter=0;
if(turnCounter == 0){
char enemy = 'X';
do
{
std::cout << "Please choose an unused square from the grid 1-9" << std::endl;
std::cin >> number;
if(table[number-1]=='O'|| table[number-1]=='X'){
std::cout << "I am sorry but that square is already in use," << std::endl;
}
}
while((number < 1 || number > 9) || (table[number-1] == 'X')|| (table[number-1] == 'O'));
table[number-1]='O';
drawTable(table);
std::cout << "Player 1 Moved......Player 2's Turn" << std::endl;
gameRegulations(table, turnCounter);
if(turnCounter == 0){
location2(location, table);
}
}
}
void location2(int *location, char table[]){
int number;
int turnCounter=1;
if(turnCounter == 1){
char enemy = 'O';
do
{
std::cout << "Please choose an unused square from the grid 1-9" << std::endl;
std::cin >> number;
if(table[number-1]=='O'|| table[number-1]=='X'){
std::cout << "I am sorry but that square is already in use," << std::endl;
}
}
while((number < 1 || number > 9) || (table[number-1] == 'X')|| (table[number-1]== 'O'));
table[number-1]='X';
std::cout << "Player 2 Moved......Player 1's Turn" << std::endl;
*location=number-1;
drawTable(table);
gameRegulations(table, turnCounter);
if(turnCounter == 1){
location1(location, table);
}
}
}
void drawTable(char table[])
{
std::cout << "=======" << std::endl;
std::cout << "|" << table[0] << "|" << table[1] << "|" << table[2] << "|" << std::endl;
std::cout << "|" << "=====" << "|" << std::endl;
std::cout << "|" << table[3] << "|" << table[4] << "|" << table[5] << "|" << std::endl;
std::cout << "|" << "=====" << "|" << std::endl;
std::cout << "|" << table[6] << "|" << table[7] << "|" << table[8] << "|" << std::endl;
std::cout << "=======" << std::endl;
}
void gameRegulations(char table[], int *turnCounter){
if((table[0]=='O' && table[1]=='O' && table[2]=='O') || (table[3]=='O' && table[4]=='O' && table[5]=='O') || (table[6]=='O' && table[7]=='O' && table[8]=='O')
||(table[0]=='X' && table[1]=='X' && table[2]=='X') || (table[3]=='X' && table[4]=='X' && table[5]=='X') || (table[6]=='X' && table[7]=='X' && table[8]=='X'))
{
std::cout << "Player " << turnCounter+1 << " Has Won the Game with Horizontal Win" << std::endl;
for(int i = 0;i<9;i++)
{
table[i]='*';
}
*turnCounter=2;
}
if((table[0]=='O' && table[3]=='O' && table[6]=='0') || (table[1]=='O' && table[4]=='O' && table[7]=='O') || (table[2]=='O' && table[5]=='O' && table[8]=='O')
|| (table[0]=='X' && table[3]=='X' && table[6]=='X') || (table[1]=='X' && table[4]=='X' && table[7]=='X') || (table[2]=='X' && table[5]=='X' && table[8]=='X'))
{
std::cout << "Player #" << turnCounter+1 << " Has Won the Game with Vertical Win" << std::endl;
for(int i = 0;i<9;i++)
{
table[i]='*';
}
*turnCounter=2;
}
if((table[0]=='O' && table[4]=='O' && table[8]=='O') || (table[2]=='O' && table[4]=='O' && table[6]=='O')
|| (table[0]=='X' && table[4]=='X' && table[8]=='X') || (table[2]=='X' && table[4]=='X' && table[6]=='X'))
{
std::cout << "Player #" << turnCounter+1 << " Has Won the Game with Diagonal Win" << std::endl;
for(int i = 0;i<9;i++)
{
table[i]='*';
}
*turnCounter=2;
}
}
|