6 Replies - 1843 Views - Last Post: 29 November 2007 - 03:21 PM Rate Topic: -----

#1 chickenkid11  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 28-November 07

Tic tac toe program

Post icon  Posted 28 November 2007 - 08:47 AM

Hey guys, I am majoring in computer science, and have an assignment to create a simple tic tac toe program. I am still kind of new at programming though. I got started on it, and got the pieces and board to display, but I want to be sure I am headed in the right direction. I am kind of stuck though, so any input would be appreciated.

Thanks,

Ryan

Here is the code:

// Tic Tac Toe 1.0
// Ryan Vowell
// Programming Fundamentals 1

#include <iostream>

char board[3][3] = { {0,0,0} , {0,0,0} , {0,0,0} }; //keeps track of X's and O's
void display(); // displays board
bool checkForWin(); // checks for win

main()
{
	  int row;
	  int column;
	  bool win = false;
	  display();
	  while(win == false)
	  {
				std::cout << "Player One's turn - " << std::endl; //Player One
				std::cout << "Row: ";
				std::cin  >> row;
				std::cout << "Column: ";
				std::cin  >> column;
				board [row - 1][column - 1] = 'X';
				display();
				checkForWin();
				
				std::cout << "Player Two's turn - " << std::endl; // Player Two
				std::cout << "Row: ";
				std::cin  >> row;
				std::cout << "Column: ";
				std::cin  >> column;
				board [row - 1][column - 1] = 'O';
				display();
				checkForWin();
	  };
	  
	  return 0;
}


void display() //board 
{
	std::cout<<"\n\t\t				1   2   3\n"<<std::endl;
	std::cout<<"\t\t			 1  "<<board[0][0]<<" | "<<board[0][1]<<" | "<<board[0][2]<<std::endl;
	std::cout<<"\t\t			   ---|---|---\n";
	std::cout<<"\t\t			 2  "<<board[1][0]<<" | "<<board[1][1]<<" | "<<board[1][2]<<std::endl;
	std::cout<<"\t\t			   ---|---|---\n";
	std::cout<<"\t\t			 3  "<<board[2][0]<<" | "<<board[2][1]<<" | "<<board[2][2]<<"\n\n\n";
}

bool checkForWin() //checks for win
{
	 bool win;
	 
	 return win;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Tic tac toe program

#2 rockstar_  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 32
  • View blog
  • Posts: 189
  • Joined: 16-October 06

Re: Tic tac toe program

Posted 28 November 2007 - 09:19 AM

View Postchickenkid11, on 28 Nov, 2007 - 08:47 AM, said:

Hey guys, I am majoring in computer science, and have an assignment to create a simple tic tac toe program. I am still kind of new at programming though. I got started on it, and got the pieces and board to display, but I want to be sure I am headed in the right direction. I am kind of stuck though, so any input would be appreciated.
I think it would be most helpful to explain what you're stuck on. I looked at your code, and I can see a few improvements, but they may be things that you have only stubbed out, and you're working on other parts of the game for now.

For instance, your while loop should look like this:
	  while(win == false)
	  {
				std::cout << "Player One's turn - " << std::endl; //Player One
				std::cout << "Row: ";
				std::cin  >> row;
				std::cout << "Column: ";
				std::cin  >> column;
				board [row - 1][column - 1] = 'X';
				display();
				checkForWin();
				
				std::cout << "Player Two's turn - " << std::endl; // Player Two
				std::cout << "Row: ";
				std::cin  >> row;
				std::cout << "Column: ";
				std::cin  >> column;
				board [row - 1][column - 1] = 'O';
				display();
				win = checkForWin();
	  };
When you run your checkForWin() function, it's returning a variable not used, and your current while doesn't have the opportunity to change the value of win, so it'll loop forever. Also, checkForWin() isn't returning anything, because you only initialized the variable, you haven't assigned a value to it. Make it look like this:
bool checkForWin() //checks for win
{
	 bool win = FALSE;
	 
	 return win;
}

I also don't see any way to enter moves, only to display them. Maybe you're still working through that. Your datastructures are okay, and it looks like you're getting the concepts. Where are you stuck?
Was This Post Helpful? 0
  • +
  • -

#3 chickenkid11  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 28-November 07

Re: Tic tac toe program

Posted 28 November 2007 - 10:14 AM

I haven't gotten to the checkForWin function yet. My problem right now is kind of hard to describe. I can have the user enter the values and the program will display them, but I don't know how to get set the program up so that if X's win, it will say "Player one wins" or something like that. I need to make the program know whose turn it is at the time so if that player wins, the program will know what to return.
Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5643
  • View blog
  • Posts: 12,359
  • Joined: 16-October 07

Re: Tic tac toe program

Posted 28 November 2007 - 11:21 AM

I'd use a nested loop for the display. Professors dig loops.
Was This Post Helpful? 0
  • +
  • -

#5 chickenkid11  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 28-November 07

Re: Tic tac toe program

Posted 28 November 2007 - 01:51 PM

You mean instead of the while loop? Not sure what you mean
Was This Post Helpful? 0
  • +
  • -

#6 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5643
  • View blog
  • Posts: 12,359
  • Joined: 16-October 07

Re: Tic tac toe program

Posted 28 November 2007 - 02:18 PM

View Postchickenkid11, on 28 Nov, 2007 - 03:51 PM, said:

You mean instead of the while loop? Not sure what you mean


I was half joking, but it's true. Repeting code is never viewed favorably.

For the board, it's 3x3, so:
for(int row=0; row<3; row++) {
   for(int col=0; col<3 col++) {
// show a cell
   }
// make a new row e.g. cout << endl;
}


Was This Post Helpful? 0
  • +
  • -

#7 chickenkid11  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 28-November 07

Re: Tic tac toe program

Posted 29 November 2007 - 03:21 PM

Alright, I got it working. All I have to do now is do the checkForWin function and I'm done. Thanks for all the help guys!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1