Requires:
variables, data types, and numerical operators
basic input/output
logic (if statements, switch statements)
loops (for, while, do-while)
arrays
Make a two player tic tac toe game.
★ Modify the program so that it will announce when a player has won the game (and which player won, x or o)
★★ Modify the program so that it is a one player game against the computer (with the computer making its moves randomly)
★★★★ Modify the program so that anytime the player is about to win (aka, they have 2 of 3 x's in a row, the computer will block w/ an o)
I wrote most of the program all ready. I am a beginner, so I had to do a lot of research to make this. It is not near to completion just yet. I wrote a function
int ImputMove (int move, bool p, char b[])
, that imputs the move into the array,
int CheckMove (int move, bool p, char a[])
, that checks to see if a player has won yet, and
void ShowBoard()
, that shows the board before the user imputs their position. I am not sure how this all connects to make the tic tac toe game. These were the features that most people included in their c++ tic tac toe games. I am little lost on where to go with this code, even though I am close to being finished. I'd appreciate some divine guidance lol! thanks a lot :)
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 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
|
// Tic Tac Toe.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
//Declare Global Variables
string Board[9] = {"0", "1", "2", "3", "4", "5", "6", "7", "8"};
//Declare Functions
void ShowBoard();
int ImputMove (int move, bool p, char b[]);
int CheckMove (int move, bool p, char a[]);
int _tmain(int argc, _TCHAR* argv[])
{
//Declare local Variables
string Player_1_Name;
string Player_2_Name;
int Move;
bool result = 1;
//Imput player names
cout << "Player 1: Please enter your name.\n";
cin >> Player_1_Name;
cout << "Player 2: Please enter your name.\n";
cin >> Player_2_Name;
//Show board
ShowBoard();
// Tell which player to move
while(result == true)
{
cout << Player_1_Name << ": It's your move." << endl;
cout << "Enter the number of the spot where you'd like to move." << endl;
cin >> Move;
cout << Player_2_Name << ": It's your move." << endl;
cout << "Enter the number of the spot where you'd like to move." << endl;
cin >> Move;
ImputMove (int move, bool a, char b[]);
CheckMove (int move, bool b, char a[]);
result = CheckMove (int move, bool b, char a[]);
}
system("PAUSE");
return 0;
}
void ShowBoard()
{
cout << endl;
cout << Board[0] << " | " << Board[1] << " | " << Board[2] << endl;
cout << "--|---|--" << endl;
cout << Board[3] << " | " << Board[4] << " | " << Board[5] << endl;
cout << "--|---|--" << endl;
cout << Board[6] << " | " << Board[7] << " | " << Board[8] << endl;
cout << endl;
}
int ImputMove (int move, bool p, char b[])
{
if(p == true)
{
switch(move)
{
case 1:
b[0] = 'X';
break;
case 2:
b[1] = 'X';
break;
case 3:
b[2] = 'X';
break;
case 4:
b[3] = 'X';
break;
case 5:
b[4] = 'X';
break;
case 6:
b[5] = 'X';
break;
case 7:
b[6] = 'X';
break;
case 8:
b[7] = 'X';
break;
case 9:
b[8] = 'X';
break;
default:
cout << "Invalid Move!\n";
break;
}
}
else
{
switch(move)
{
case 1:
b[0] = 'O';
break;
case 2:
b[1] = 'O';
break;
case 3:
b[2] = 'O';
break;
case 4:
b[3] = 'O';
break;
case 5:
b[4] = 'O';
break;
case 6:
b[5] = 'O';
break;
case 7:
b[6] = 'O';
break;
case 8:
b[7] = 'O';
break;
case 9:
b[8] = 'O';
break;
default:
cout << "Invalid Move !\n";
break;
}
}
}
int CheckMove (int move, bool p, char a[])
{
if(p== false)
{
if((a[0] == 'X' && a[1] == 'X' && a[2] == 'X'))
{
cout << "PLAYER 1 WINS" << endl;
return 0;
}
if((a[3] == 'X' && a[4] == 'X' && a[5] == 'X'))
{
cout << "PLAYER 1 WINS" << endl;
return 0;
}
if((a[6] == 'X' && a[7] == 'X' && a[8] == 'X'))
{
cout << "PLAYER 1 WINS" << endl;
return 0;
}
if((a[0] == 'X' && a[3] == 'X' && a[6] == 'X'))
{
cout << "PLAYER 1 WINS" << endl;
return 0;
}
if((a[1] == 'X' && a[4] == 'X' && a[7] == 'X'))
{
cout << "PLAYER 1 WINS" << endl;
return 0;
}
if((a[2] == 'X' && a[5] == 'X' && a[8] == 'X'))
{
cout << "PLAYER 1 WINS" << endl;
return 0;
}
if((a[0] == 'X' && a[4] == 'X' && a[8] == 'X'))
{
cout << "PLAYER 1 WINS" << endl;
return 0;
}
if((a[2] == 'X' && a[4] == 'X' && a[6] == 'X'))
{
cout << "PLAYER 1 WINS" << endl;
return 0;
}
if((a[0] == 'O' && a[1] == 'O' && a[2] == 'O'))
{
cout << "PLAYER 2 WINS" << endl;
return 0;
}
if((a[3] == 'O' && a[4] == 'O' && a[5] == 'O'))
{
cout << "PLAYER 2 WINS" << endl;
return 0;
}
if((a[6] == 'O' && a[7] == 'O' && a[8] == 'O'))
{
cout << "PLAYER 2 WINS" << endl;
return 0;
}
if((a[0] == 'O' && a[3] == 'O' && a[6] == 'O'))
{
cout << "PLAYER 2 WINS" << endl;
return 0;
}
if((a[1] == 'O' && a[4] == 'O' && a[7] == 'O'))
{
cout << "PLAYER 2 WINS" << endl;
return 0;
}
if((a[2] == 'O' && a[5] == 'O' && a[8] == 'O'))
{
cout << "PLAYER 2 WINS" << endl;
return 0;
}
if((a[0] == 'O' && a[4] == 'O' && a[8] == 'O'))
{
cout << "PLAYER 2 WINS" << endl;
return 0;
}
if((a[2] == 'O' && a[4] == 'O' && a[6] == 'O'))
{
cout << "PLAYER 2 WINS" << endl;
return 0;
}
}
}
|