1 Replies - 2615 Views - Last Post: 04 April 2009 - 05:42 PM Rate Topic: -----

#1 maybnxtseasn  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 87
  • Joined: 04-April 09

simple tic tac toe program help....

Posted 04 April 2009 - 04:25 PM

BTW this is not my homework...this is purely for learning....i started this tictactoe program and am almost done....thing is i dont know what the hell is wrong with it....my scanf does not pass my variable correctly to my singleplayerturn function....im not sure im using the scanf function improperly or if my pointers are used incorrectly... thx for the help!!! dont change anything else i wanna do the rest just help me fix my problem
//Author: 
//Description: Simple Tic Tac Toe game
//
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define MAXNUMBERS 2


int singlePlayer(char *,char *,char [3][3]);										 //prototype for singleplayer
void singleplayerTurn(int *,char *,char *,char [3][3]);							  // prototype for singleplayer turn picking function
//void multiPlayer(char,int);														//prototype for multiplayer

int main()
	{
		char board[3][3]= { '-','-','-',									 //tic tac toe board
							  '-','-','-',
							'-','-','-'};
		char player1;
		char player2;
		char computer;
		
		int playerFirst;													 //1 = player goes first	2 = computer goes first
		
		int menuselect;													  

			int holdScreen;													  //for testing
		char dummykey;									  
		
		printf("\nWelcome to Tic Tac Toe");				   
		printf("\n----------------------");
		printf("\n1.) SinglePlayer	  ");
		printf("\n2.) Multiplayer	   ");
		printf("\n3.) Exit			 \n");

		
		scanf("%d",&menuselect);
		
		switch(menuselect)	 
		{									 
			case 1:
				printf("\nSinglePlayer Mode Selected.....LOADING");
				printf("\n--------------------------------------");
				playerFirst = singlePlayer(&player1,&computer,board);					   //single player function return value assigned to playerFirst
				singleplayerTurn(&playerFirst,&player1,&computer,board);					//depending on player turn make a rotation for it...let them input 'x' or 'o'....check to see if winner is found
								break;
			case 2:
				printf("\nMultiplayer Mode Selected.....LOADING");						  //not yet implemented
				printf("");
		 			  //multiPlayer(player1,player2,board[][]);
								break;
			case 3:
				printf("\nExiting....");													//exit program
				break;
		}
	  
		return (0);
	}

//---------------------------------------------------
//SinglePlayer Function of tic tac toe game
//---------------------------------------------------
int singlePlayer(char *playerOne,char *computerOne,char board[3][3])											 //single player function to see who's 'x' and 'o'.....also check who goes first
	{
		int randomnumber;					   
		int randvalue;   
		char dummy;							  
		srand(time(NULL));			
		
		int playerFirst;				 
	
		printf("\nWhat would you like to be X's or O's?\n");
		scanf("%c%c",&dummy,&playerOne);
													  //allow user to input value 'x' or 'o'....dummykey eats up ENTER key

		if (*playerOne == 'x')																		 //logical if to assign a computer 'x' or 'o'
			{					 
			*computerOne = 'o';				 
			}
		else
			{ 
			*computerOne = 'x';
			}
		printf("\nNow lets decide who gets to go first!");
		printf("\n-------------------------------------");
		printf("\nPick a number between 1-2		 \n");
		scanf("%d",&randomnumber);
		
		randvalue = 1 + (int)rand() % MAXNUMBERS;				
		
		if (randomnumber == randvalue)
			{
				printf("\nCongratulations you guessed right on the dot! u get to go first!");
				playerFirst = 1;
			}
		else
			{
				printf("\nBetter luck next time! Computer gets to go first :( .");
				playerFirst = 0;
			}
		printf("\nThe number was....%d",randvalue);
		printf("\nLet the Singleplayer game begin!!!!");
		return (playerFirst);
	}


//---------------------------------------------------
//SinglePlayer Turn Function 
//---------------------------------------------------
void singleplayerTurn(int *playerFirst,char *playerOneagain,char *computerOneagain,char board[3][3])
	{

	int x,i;
	int delay;
	int holdScreen;								  // count thru loop must = 9 
	int row;
	int column;
	int randomnumber;								// user picked random number for deciding who goes first!
	int randvalue;								   // program generated random number
					
			  for (x = 0;x >= 0;x++)
					{
//
//Players Turn rotation
//	
	  if (*playerFirst == 1)
	  {		  
					   printf("\n%c %c %c\n%c %c %c\n%c %c %c\n\n",board[0][0],board[0][1],board[0][2],board[1][0],board[1][1],board[1][2],board[2][0],board[2][1],board[2][2]);
					   printf("\n Where would you like to place your %c? ",*playerOneagain);
					   scanf("%d%d",&row,&column);
					   row -= 1;
					   column -=1;
					  
		 if ( (row == 0 && column == 0) && (board[0][0] != 'x') && (board[0][0] != 'o') )
						  {
							   board[0][0] = *playerOneagain;
							   *playerFirst = 0;
							   continue;
						  }
					   if ( (row == 0 && column == 1) && (board[0][1] != 'x') && (board[0][1] != 'o') )
						  {
							   board[0][1] = *playerOneagain;
							   *playerFirst = 0;
							   continue;
						  }
					   if ( (row == 0 && column == 2) && (board[0][2] != 'x') && (board[0][2] != 'o') )
						  {
							   board[0][2] = *playerOneagain;
							   *playerFirst = 0;
							   continue;
						  }
					   if ( (row == 1 && column == 0) && (board[1][0] != 'x') && (board[1][0] != 'o') )
						  {
							   board[1][0] = *playerOneagain;
							   *playerFirst = 0;
							   continue;
						  }
					   if ( (row == 1 && column == 1) && (board[1][1] != 'x') && (board[1][1] != 'o') )
						  {
							   board[1][1] = *playerOneagain;
							   *playerFirst = 0;
							   continue;
						  }
					   if ( (row == 1 && column == 2) && (board[1][2] != 'x') && (board[1][2] != 'o') )
						  {
							   board[1][2] = *playerOneagain;
								*playerFirst = 0;
								continue;
						  }
					   if ( (row == 2 && column == 0) && (board[2][0] != 'x') && (board[2][0] != 'o') )
						  {
							   board[2][0] = *playerOneagain;
							   *playerFirst = 0;
							   continue;
						  }
					   if ( (row == 2 && column == 1) && (board[2][1] != 'x') && (board[2][1] != 'o') )
						  {
							   board[2][1] = *playerOneagain;
							   *playerFirst = 0;
							   continue;
						  }
					   if ( (row == 2 && column == 2) && (board[2][2] != 'x') && (board[2][2] != 'o') )
						  {
							   board[2][2] = *playerOneagain;
							   *playerFirst = 0;
							   continue;
						  }
					   else
						   {
							   printf("\nInvalid row and column entered....or the spot is taken");
							   printf("\n!please pick a new spot to put your %c",*playerOneagain);
							   continue;
						   }
					   	
					 }
//
//Computers Turn rotation
//					   	
	  if (*playerFirst == 0)
	  {			 
					   	srand(time(NULL));				 // RNG seed from time
						   
					   	for(i = 0; i <= 10000; i++) 
							  {

							  }
							row = 0 + (int)rand() % 2;				// generates a random value between 0-2
						   	column = 0 + (int)rand() % 2;				// generates a random value between 0-2

					   	
					   	
					   if ( (row == 0 && column == 0) && (board[0][0] != 'x') && (board[0][0] != 'o') )
						  {
							   board[0][0] = *computerOneagain;
								printf("\n%c %c %c\n%c %c %c\n%c %c %c\n\n",board[0][0],board[0][1],board[0][2],board[1][0],board[1][1],board[1][2],board[2][0],board[2][1],board[2][2]);
							   *playerFirst = 1;
							   continue;
						  }
					   if ( (row == 0 && column == 1) && (board[0][1] != 'x') && (board[0][1] != 'o') )
						  {
							   board[0][1] = *computerOneagain;
								printf("\n%c %c %c\n%c %c %c\n%c %c %c\n\n",board[0][0],board[0][1],board[0][2],board[1][0],board[1][1],board[1][2],board[2][0],board[2][1],board[2][2]);
							   *playerFirst = 1;
							   continue;
						  }
					   if ( (row == 0 && column == 2) && (board[0][2] != 'x') && (board[0][2] != 'o') )
						  {
							   board[0][2] = *computerOneagain;
								printf("\n%c %c %c\n%c %c %c\n%c %c %c\n\n",board[0][0],board[0][1],board[0][2],board[1][0],board[1][1],board[1][2],board[2][0],board[2][1],board[2][2]);
							   *playerFirst = 1;
							   continue;
						  }
					   if ( (row == 1 && column == 0) && (board[1][0] != 'x') && (board[1][0] != 'o') )
						  {
							   board[1][0] = *computerOneagain;
								printf("\n%c %c %c\n%c %c %c\n%c %c %c\n\n",board[0][0],board[0][1],board[0][2],board[1][0],board[1][1],board[1][2],board[2][0],board[2][1],board[2][2]);
							   *playerFirst = 1;
							   continue;
						  }
					   if ( (row == 1 && column == 1) && (board[1][1] != 'x') && (board[1][1] != 'o') )
						  {
							   board[1][1] = *computerOneagain;
								printf("\n%c %c %c\n%c %c %c\n%c %c %c\n\n",board[0][0],board[0][1],board[0][2],board[1][0],board[1][1],board[1][2],board[2][0],board[2][1],board[2][2]);
							   *playerFirst = 1;
							   continue;
						  }
					   if ( (row == 1 && column == 2) && (board[1][2] != 'x') && (board[1][2] != 'o') )
						  {
							   board[1][2] = *computerOneagain;
								printf("\n%c %c %c\n%c %c %c\n%c %c %c\n\n",board[0][0],board[0][1],board[0][2],board[1][0],board[1][1],board[1][2],board[2][0],board[2][1],board[2][2]);
							   *playerFirst = 1;
							   continue;
						  }
					   if ( (row == 2 && column == 0) && (board[2][0] != 'x') && (board[2][0] != 'o') )
						  {
							   board[2][0] = *computerOneagain;
								printf("\n%c %c %c\n%c %c %c\n%c %c %c\n\n",board[0][0],board[0][1],board[0][2],board[1][0],board[1][1],board[1][2],board[2][0],board[2][1],board[2][2]);
							   *playerFirst = 1;
							   continue;
						  }
					   if ( (row == 2 && column == 1) && (board[2][1] != 'x') && (board[2][1] != 'o') )
						  {
							   board[2][1] = *computerOneagain;
								printf("\n%c %c %c\n%c %c %c\n%c %c %c\n\n",board[0][0],board[0][1],board[0][2],board[1][0],board[1][1],board[1][2],board[2][0],board[2][1],board[2][2]);
							   *playerFirst = 1;
							   continue;
						  }
					   if ( (row == 2 && column == 2) && (board[2][2] != 'x') && (board[2][2] != 'o') )
						  {
							   board[2][2] = *computerOneagain;
								printf("\n%c %c %c\n%c %c %c\n%c %c %c\n\n",board[0][0],board[0][1],board[0][2],board[1][0],board[1][1],board[1][2],board[2][0],board[2][1],board[2][2]);
							   *playerFirst = 1;
							   continue;
						  }
					   else
						   {
							   continue;
						   }
							 *playerFirst = 0;
					   	
	  }
	  }
			
}
/*
//---------------------------------------------------
//Multiplayer Function of tic tac toe game
//---------------------------------------------------
void multiPlayer(char player1,char player2,char board[])
	{

		return (0);
	}
*/



Is This A Good Question/Topic? 0
  • +

Replies To: simple tic tac toe program help....

#2 bsaunders  Icon User is offline

  • D.I.C Addict

Reputation: 44
  • View blog
  • Posts: 571
  • Joined: 18-January 09

Re: simple tic tac toe program help....

Posted 04 April 2009 - 05:42 PM

Do you notice a problem in the singlePlayer function?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1