ID:182887
 
Woo, the past few days I have been learning C++ and last night/this morning I created a simple Tic-Tac-Toe program. There are no graphics or anything, but it works 100%!

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.
I'd be very weary of this...
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
In response to Flame Sage
Flame Sage wrote:
Also, when you enter something your not supposed to (ex: 35), it freaks out and spams you to death.

Yeah, I wasn't sure how to put in the coordinates, so I put [1][1] and it wigged out <_< rofl
In response to Flame Sage
Flame Sage wrote:
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

What you posted isn't indicative of a memory leak. In fact, I can verify that his program isn't leaking any memory. It's indicative of a loop taking up a LOT of CPU.
In response to Flame Sage
Yeah I fixed the enterting a number too high thing...But BYOND wasn't letting me login so I couldnt reupload or anything. Heh.
In response to Jamesburrow
Jamesburrow wrote:
Flame Sage wrote:
Also, when you enter something your not supposed to (ex: 35), it freaks out and spams you to death.

Yeah, I wasn't sure how to put in the coordinates, so I put [1][1] and it wigged out <_< rofl

I have no idea how to fix that... Any ideas? If you enter like 0,0 it freaks out too.
In response to Dession
Not that great at C++, but it appears you are using a loop... set the value to null if it is invalid?
In response to Dession
You could try reading it as a string and then converting it to an integer. You should then be able to catch the exception that is raised if what's entered is not an integer. I found a fairly decent-looking forum thread on string conversion at http://www.thescripts.com/forum/thread132109.html. You can catch exceptions using a try..catch block, which you could place in a loop in a function, i.e.