Sign in to follow this  

Tic Tac Toe Help

This topic is 3677 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Modules.h
#include <iostream>
#include <time.h>
using namespace std;

void DisplayGUI()
{
 char arr[] = {'1','2','3','4','5','6','7','8','9','10'};

 cout << "---------------------------------" << endl;
 cout << "-          -         -          -" << endl;
 cout << "-          -         -          -" << endl;
 cout << "-     " << arr[6] << "    -     " << arr[7] << "   -      " << arr[8] << "   -" << endl;
 cout << "-          -         -          -" << endl;
 cout << "-          -         -          -" << endl;
 cout << "---------------------------------" << endl;
 cout << "-          -         -          -" << endl;
 cout << "-          -         -          -" << endl;
 cout << "-     " << arr[3] << "    -     " << arr[4] << "   -      " << arr[5] << "   -" << endl;
 cout << "-          -         -          -" << endl;
 cout << "-          -         -          -" << endl;
 cout << "---------------------------------" << endl;
 cout << "-          -         -          -" << endl;
 cout << "-          -         -          -" << endl;
 cout << "-     " << arr[0] << "    -     " << arr[1] << "   -      " << arr[2] << "   -" << endl;
 cout << "-          -         -          -" << endl;
 cout << "-          -         -          -" << endl;
 cout << "---------------------------------" << endl;
}


void SelectMarker()
{
	char player;
	char CPU;
	cin >> player;
	{
	if (player == 'x'||player =='X')
	{
	 player = 'X';
	 CPU = 'O';
	}
	else if (player == 'o'||player == 'O')
	{
		player = 'O';
		CPU = 'X';
	}
	};

	cout << "Player 1 is: " << player << endl;
	cout << "Computer is : " << CPU << endl;

}

void GameInstructions()
{
	//display game instruction at game load
}

int Oplayer()
{
	 char arr[] = {'1','2','3','4','5','6','7','8','9','10'};
     int i;
     
     cout << "\n\n Choose a place to put an O.";
     cin >> i;
     
     switch(i)
        {
              case 0: arr[0] = 'O'; break;
              case 1: arr[1] = 'O'; break;
              case 2: arr[2] = 'O'; break;
              case 3: arr[3] = 'O'; break;
              case 4: arr[4] = 'O'; break;
              case 5: arr[5] = 'O'; break;
              case 6: arr[6] = 'O'; break;
              case 7: arr[7] = 'O'; break;
              case 8: arr[8] = 'O'; break;
              default: cout << "\n Invalid input"; 
        }
      
	 return (DisplayGUI);
}


/*notes

ask user to input number associated with any square
789
456
123

player choose as 0 for number slot 1, then O will be placed at one(arr[0])

*/
main.cpp
#include <iostream>
#include "Modules.h"
using namespace std;

int Oplayer();
void SelectMarker();
void DisplayGUI();


void main()
{
	DisplayGUI();
	cout << "Player 1 X or O?" << endl;
	SelectMarker();
	Oplayer();
	//MakeMove(arr);
}
I am trying to get this to work but I'm at a loss. right now I need help and I'm trying to take input from user, use that info to correspond to a grid on the tic tac toe board and display it

Share this post


Link to post
Share on other sites

First of all, one problem that I see is that you have your main storage for your game declared and initialized in different scopes *. It would also seem that important information needed for the game is being stored this way, such as "char player" and "char CPU". To make these variables usable, I would suggest is to make them global variables. (Although global variables are highly discouraged, I think that at your level of programming, it could serve beneficial.)


switch(i)
{
case 0: arr[0] = 'O'; break;
case 1: arr[1] = 'O'; break;
case 2: arr[2] = 'O'; break;
case 3: arr[3] = 'O'; break;
case 4: arr[4] = 'O'; break;
case 5: arr[5] = 'O'; break;
case 6: arr[6] = 'O'; break;
case 7: arr[7] = 'O'; break;
case 8: arr[8] = 'O'; break;
default: cout << "\n Invalid input";
}


This can be simplified, btw:


if ( i >= '0' && i <= '9' )
arr[ i - '0' ] = 'O';
else
cout << "\n Invalid input";



And you may want to check to make sure that there isn't already a marker there:

if ( i >= '0' && i <= '9' )
if ( arr[ i - '0' ] != 'X' && arr[ i - '0' ] = 'O' )
arr[ i - '0' ] = 'O';
else
cout << "\n There is already a marker in this spot!";
else
cout << "\n Invalid input";



You may also want to return a value so the program knows weather or not the player's pick was valid:


if ( i >= '0' && i <= '9' )
if ( arr[ i - '0' ] != 'X' && arr[ i - '0' ] = 'O' )
arr[ i - '0' ] = 'O';
else
{
cout << "\n There is already a marker in this spot!";
return -1;
}
else
{
cout << "\n Invalid input";
return -1;
}
/* DisplayGUI would be more suited to be handled
by the main game function, rather than this function */

// DisplayGUI();

return 0;



I also don't see a main game loop where the player and computer take turns making moves:

void main()
{
DisplayGUI();

// Begin the game.
// Decide who's who:
cout << "Player 1 X or O?" << endl;
SelectMarker();

// Main game loop
bool game_is_over = false;
while ( game_is_over == false )
{
// Force the player to make a correct move
while ( Oplayer() == -1 ) {}
/* computer_move(); */

DisplayGUI();

if ( /* no_more_squares_left() */ )
{
cout << "Cats game!\n";
game_is_over = true;
}
else
{
int winner = /* check_for_winner() */;
if ( winner )
{
if ( winner == 1 )
cout << "You win!\n";
else
cout << "The computer won.\n";
game_is_over = true;
}
}
}
}



I hope this shows you the basics of game logic, and I also hope that you can learn from this code.

* http://en.wikipedia.org/wiki/Variable#Scope_and_extent


Share this post


Link to post
Share on other sites

This topic is 3677 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this