2 Replies - 7900 Views - Last Post: 07 August 2012 - 11:16 AM Rate Topic: -----

#1 kevoh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 07-August 12

Tic-tac-toe challenge

Posted 07 August 2012 - 10:10 AM

I'm done with my tic-tac-toe program, but I've decided to ask a simple question to the winner of the match. Do I use a while loop? I'm a little stuck, thanks for your help. Also, how can I make my code neater/shorter?


#include <iostream>

using namespace std;

bool checkWin (char);


void displayMap ();


char display [5][5] = { { ' ', '|', ' ', '|', ' ' }, { '-', '+', '-', '+', '-' }, { ' ', '|', ' ', '|', ' ' }, { '-', '+', '-', '+', '-' }, { ' ', '|', ' ', '|', ' ' } };

int main ()
{
    system("color f4");
	char map [3][3] = { { ' ', ' ', ' ' }, { ' ', ' ', ' ' }, { ' ', ' ', ' ' } },
		player[2] = { ' ', ' ' },
		again = 'y';

	bool gameWon = false;
	
	while (again != 'n')
	{
		int turns = 0;
		cout << "Player 1 enter the character you are going to use (no spaces): ";
		cin >> player[0];
		while (player[0] == ' ' || player[0] == '	')
		{
			cout << "You entered a space (no spaces), re-enter a new character: ";
			cin >> player[0];
		}
		cout << endl << "Player 2 enter the character you are going to use (no spaces): ";
		cin >> player[1];
		while (player[1] == ' ' || player[1] == '	')
		{
			cout << "You entered a space (no spaces), re-enter a new character: ";
			cin >> player[1];
		}
		while (turns != 9 && !gameWon)
		{
			unsigned short x, y;
			cout << endl << endl;
			bool p1turn = true, p2turn = true;
			while (p1turn && turns != 9)
			{
				p1turn = false;
				displayMap();
				cout << endl << endl << "Player 1: Enter 2 numbers (1-3) separated by a space to use the place: ";
				cin >> x >> y;
				while (x > 3 || x < 1 || y > 3 || y < 1)
				{
					cout << "You must enter numbers from 1 to 3, please re-enter them: ";
					cin >> x >> y;
				}
				if (map[x - 1][y - 1] != ' ')
				{
					p1turn = true;
					cout << "You entered a coordinate that was already taken." << endl << endl;
				}
				else
				{
					map[x - 1][y - 1] = player[0];
					x = (x * 2) - 1;
					y = (y * 2) - 1;
					display[x - 1][y - 1] = player[0];
					if (checkWin(player[0])) gameWon = true;
					turns++;
				}
			}
			while (p2turn && turns != 9 && !gameWon)
			{
				p2turn = false;
				cout << endl;
				displayMap();
				cout << endl << endl << "Player 2:  Enter 2 numbers (1-3) separated by a space to use the place: ";
				cin >> x >> y;
				while (x > 3 || x < 1 || y > 3 || y < 1)
				{
					cout << "You must enter numbers from 1 to 3, please re-enter them: ";
					cin >> x >> y;
				}
				if (map[x - 1][y - 1] != ' ')
				{
					p2turn = true;
					cout << "You entered a coordinate that was already taken." << endl << endl;
				}
				else
				{
					map[x - 1][y - 1] = player[1];
					x = (x * 2) - 1;
					y = (y * 2) - 1;
					display[x - 1][y - 1] = player[1];
					if (checkWin(player[1])) gameWon = true;
					turns++;
				}
			}
		}
		cout << endl << endl;
		displayMap();
		if (checkWin(player[0]))
			cout << endl << endl << "And the winner is: Player 1!!!" << endl << endl;
		else if (checkWin(player[1]))
			cout << endl << endl << "And the winner is: Player 2!!!" << endl << endl;
		else cout << endl << endl << "And it looks like we have a draw folks. Cats Game." << endl << endl;
		cout << "Play again? (y/n): ";
		cin >> again;

		if (again != 'n')
		{
			for (int x = 1; x <= 3; x++)
				for (int y = 1; y <= 3; y++)
				{
					map[x - 1][y - 1] = ' ';
					display[(x * 2) - 2][(y * 2) - 2] = ' ';
				}
		}
	}

	cout << endl << endl;
	system("Pause");
	return 0;
}

bool checkWin (char player)
{
	bool result = false;
	for (int x = 0; x < 5; x += 2)
	{
		bool thisResult = true;
		for (int y = 0; y < 5; y += 2)
			thisResult = thisResult && display[x][y] == player;
		result = result || thisResult;
	}
	for (int x = 0; x < 5; x += 2)
	{
		bool thisResult = true;
		for (int y = 0; y < 5; y += 2)
			thisResult = thisResult && display[y][x] == player;
		result = result || thisResult;
	}
	bool thisResult[2] = { true, true };
	int y;
	for (int x = 0; x < 5; x += 2)
	{
		y = 4 - x;
		thisResult[0] = thisResult[0] && display[x][y] == player;
	}
	result = result || thisResult[0];
	for (int x = 0; x < 5; x += 2)
	{
		y = x;
		thisResult[0] = thisResult[0] && display[x][y] == player;
	}
	result = result || thisResult[0];
	return result;
}

void displayMap ()
{
	cout << "  1 2 3" << endl << " " << endl;
	for (int x = 0; x < 5; x++)
	{
		if (x != 1 && x != 3) cout << (x + 2) / 2 << " ";
		else cout << "   ";
		for (int y = 0; y < 5; y++)
			cout << display[x][y];
		cout << endl;
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Tic-tac-toe challenge

#2 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 858
  • View blog
  • Posts: 2,274
  • Joined: 28-March 11

Re: Tic-tac-toe challenge

Posted 07 August 2012 - 10:17 AM

This really isn't for the challenges forum, moving to C/C++
Was This Post Helpful? 0
  • +
  • -

#3 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1619
  • View blog
  • Posts: 3,068
  • Joined: 30-May 10

Re: Tic-tac-toe challenge

Posted 07 August 2012 - 11:16 AM

My first suggestion would be to do some more factoring on your 100+ line main() function.

Perhaps you could reduce main to being something like
int main ( ) {
    char again;
    do {
        playGame();
        cout << "Again?";
        cin >> again;
    } while ( again == 'y' );
    return 0;
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1