Here's a link to the program:
http://members.byond.com/Dession/files/Tic%20Tac%20Toe.exe
So basically, what do you think of it?
For all of you C++ programers out there here's my code, if there's any improvements I can make or anything please tell me so I can become more efficent and have more knowledge of C++:
#include <iostream>
using namespace std;
static const int MAP_WIDTH = 3;
static const int MAP_HEIGHT = 3;
char map[MAP_WIDTH][MAP_HEIGHT];
bool xTurn = false;
void DisplayMap()
{
int xs, ys;
cout << "Map:\n";
for (xs = 0; xs<MAP_WIDTH; xs++)
{
for(ys = 0; ys<MAP_HEIGHT; ys++)
{
cout << "[" << xs << "][" << ys << "] ";
cout << map[xs][ys] << " ";
}
cout << "\n";
}
cout << "\n\n";
}
bool IsGameWon()
{
char turn = (xTurn ? 'X' : 'Y');
return( (map[0][0] == turn && map[0][1] == turn && map[0][2] == turn) ||
(map[1][0] == turn && map[1][1] == turn && map[1][2] == turn) ||
(map[2][0] == turn && map[2][1] == turn && map[2][2] == turn) ||
(map[0][0] == turn && map[1][0] == turn && map[2][0] == turn) ||
(map[0][1] == turn && map[1][1] == turn && map[2][1] == turn) ||
(map[0][2] == turn && map[1][2] == turn && map[2][2] == turn) ||
(map[0][0] == turn && map[1][1] == turn && map[2][2] == turn) ||
(map[0][2] == turn && map[1][1] == turn && map[2][0] == turn) );
}
void MakeMove(char piece)
{
int placingX, placingY;
bool isPlaced = false;
while(!isPlaced)
{
cout << "Which X coordinate would you like to place your " << piece << "? [0-2]\n";
cin >> placingX;
cout << "Which Y coordinate would you like to place your " << piece << "? [0-2]\n";
cin >> placingY;
if(placingX > 2 || placingX <0 || placingY > 2 || placingY < 0 || map[placingX][placingY] != '-')
cout << "\nYou can't place your " << piece << " at that location! Try again.\n\n";
else if(map[placingX][placingY] == '-')
{
map[placingX][placingY] = piece;
cout << "\n\n";
isPlaced = true;
}
}
}
void GameLoop()
{
int turnCount = 0;
while(turnCount < MAP_HEIGHT*MAP_WIDTH && !IsGameWon())
{
// Incremement the turnCount and alternate the next player
turnCount++;
xTurn = !xTurn;
if(xTurn)
{
cout << "It is player X's turn.\n";
DisplayMap();
MakeMove('X');
}
else
{
cout << "It is player Y's turn.\n";
DisplayMap();
MakeMove('Y');
}
}
// Game is over, test if it was a win or a draw
if( turnCount < MAP_HEIGHT*MAP_WIDTH )
{
// It's a win
cout << (xTurn ? 'X' : 'Y') << " Wins!\n\n";
DisplayMap();
}
else
{
// It's a draw
cout << "Cat's game! No one wins.\n\n";
DisplayMap();
}
}
int main()
{
cout << "\t\tWelcome to Dession's Tic-Tac-Toe three in a row!\n\n";
int xs, ys;
for (xs = 0; xs<MAP_WIDTH; xs++)
{
for(ys = 0; ys<MAP_HEIGHT; ys++)
{
map[xs][ys] = '-';
}
}
GameLoop();
system("pause");
return 0;
}
P.S - I'm going to remake it using vectors and OOP, this was just the first run-through of the program.
EDIT - I reuploaded it and changed the code in this post.
First off, it's confusing as heck.
Also, when you enter something your not supposed to (ex: 35), it freaks out and spams you to death.
Also, when you close it, it creates a pretty bad memory leak.
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
8180 chris 25 0 2579m 2540 1840 R 77 0.1 38:49.53 Tic Tac Toe.exe