C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old April 12th, 2007, 02:14 PM
MnkyScpe MnkyScpe is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2007
Posts: 16 MnkyScpe User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 h 19 m 55 sec
Reputation Power: 0
Tic tac toe help

i need some help on a tic tac toe game... this is the code ive gotten so far
Code:
#include <iostream>
#include <stdlib.h>

using namespace std;

const int NUMBER_OF_ROWS = 3;
const int NUMBER_OF_COLUMNS = 3;

int table[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS];
int row;
int col;
char ans;
char play1;
char play2;
void main()
{
	int table[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS] ={{1, 2, 3},
													{4, 5, 6},
													{7, 8, 9}};

	for (row = 0; row < NUMBER_OF_ROWS; row++)
	{
		for (col = 0; col < NUMBER_OF_COLUMNS; col++)
			cout << table[row][col] << "______ ";
		cout << endl;
	}
	do
	{
		cout << "Would you like to play a game of tic-tac-toe?(y/n)"
			<< endl;
		cin >> ans;
		
		if (ans != char (121) && ans != char (110))
			cout << "Invalid iput, y or n." << endl;
			
		else if (ans == char (110))
			cout << "Thanks for playing!" << endl;
		
	}while (ans != char (110) && ans != char (121));

	if (ans == char (121))
		cout << "Player one, X's or O's?" << endl;
		cin >> play1;
		if (play1 == char (88))
			play2 = char (79);
		else if (play1 == char (79))
			play2 = char (88);
	
	cout << "Player 1 is " << play1 << " and Player 2 is " << play2 << endl;
}


ive been able to loop it till a valid input is found and ive continued it so that player 1 can choose x's or o's but i want to make it so that the board stays on screen and somehow updates itself with each selection of a number. also im not sure how to go about keeping the text from reprinting each time... if im confusing i can try to clarify and if its possible to get help maybe through a messanger like msn etc? im really lost and could use any help i can get, thanks! oh and im using MSV C++ compiler.

Reply With Quote
  #2  
Old April 12th, 2007, 02:33 PM
Paul820 Paul820 is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2006
Location: United Kingdom
Posts: 428 Paul820 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 Days 7 h 55 m 4 sec
Reputation Power: 13
Well you could have another function called redraw and pass it the table array after the players take each of their turns. The redraw function would just output the array of rows and columns. Before calling the redraw function, call a function to clear the screen. Is this a class assignment? If not i have some code i got off MSDN for clearing the console window. I use it. It will only work on windows though.
__________________
USE CODE TAGS: Highlight the code, click on the # button.

Reply With Quote
  #3  
Old April 12th, 2007, 02:38 PM
MnkyScpe MnkyScpe is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2007
Posts: 16 MnkyScpe User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 h 19 m 55 sec
Reputation Power: 0
yea its a class asignment and the teach is force feeding projects being the end of smemster is near, so im getting lost on this because of the other projects piling up... now i remember seeing him write the table in the global variables so he can call it later without having to recode it everytime... i dont remember what he did though, and he said it should all be one program no header files n what not if that makes any sense. So i can redraw the table? is that a c++ command or a c? i dont think hes taught that yet, but maybe he has, imma check the book and c what i can find out, oh and i know that system(cls); is the clear screen but i want to make sure the table stasy put and that the line of text asking where to go next stays as well, would i have to redraw that line of text?

Reply With Quote
  #4  
Old April 12th, 2007, 02:50 PM
Paul820 Paul820 is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2006
Location: United Kingdom
Posts: 428 Paul820 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 Days 7 h 55 m 4 sec
Reputation Power: 13
I meant pass the table for your array to a function. Can you use functions? If you can your function prototype would look like
Code:
void redrawTable( int t[][3], int columns );

your function call would be
Code:
redrawTable( table, NUMBER_OF_COLUMNS );

and your function
Code:
void redrawTable( int t[][3], int columns )
{
   for ( int i = 0; i < 3; ++i )
   {
      for ( int j = 0; j < cols; ++j )
      {
         std::cout << t[i][j];
      }

      std::cout << std::endl;
   }
}


EDIT: Corrected some typo's

Last edited by Paul820 : April 12th, 2007 at 02:58 PM.

Reply With Quote
  #5  
Old April 12th, 2007, 03:07 PM
MnkyScpe MnkyScpe is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2007
Posts: 16 MnkyScpe User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 h 19 m 55 sec
Reputation Power: 0
yea we can use functions, ok i tried to put that into my code but i guess im doing it wrong... now i use the call in the code to print it correct? so id put
Code:
redrawTable (table, NUMBER_OF_COLUMNS);
in where i ask if player 1 wants to be x's or o's but were do the other two go? do i put the function within main? and the prototype outside of main?

Reply With Quote
  #6  
Old April 12th, 2007, 03:14 PM
Paul820 Paul820 is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2006
Location: United Kingdom
Posts: 428 Paul820 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 Days 7 h 55 m 4 sec
Reputation Power: 13
Prototype goes above main. Function goes below main. Call the function only when the player has chosen a position on the board to put their O or X and you have put the O or X into the array. That way the new array will be shown with their O or X at that position. You will have to call the function twice, once for when player one has entered their position and again for player two.

Reply With Quote
  #7  
Old April 12th, 2007, 03:47 PM
MnkyScpe MnkyScpe is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2007
Posts: 16 MnkyScpe User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 h 19 m 55 sec
Reputation Power: 0
scratch that... so i got the stuff int eh right place now im just curious as to how i can make it look more like a tic tac toe board. i know it has something to do with a cout statment within the board function but again not sure how to do it.

Last edited by MnkyScpe : April 12th, 2007 at 03:56 PM. Reason: found my mistake!

Reply With Quote
  #8  
Old April 12th, 2007, 04:40 PM
MnkyScpe MnkyScpe is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2007
Posts: 16 MnkyScpe User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 h 19 m 55 sec
Reputation Power: 0
here is what my code looks like now but for some reason when i enter n it doesnt exit the loop like it should and it shouldnt start the do loop because the ans doesnt equal y
Code:
#include <iostream>
#include <stdlib.h>

using namespace std;

const int NUMBER_OF_ROWS = 3;
const int NUMBER_OF_COLUMNS = 3;

int row;
int col;
char ans;
int move1;
int move2;
int move3;
char play1;
char play2;
int table[3][3] = {1,2,3,2,2,3,3,3,3};
void redrawTable( int t[3][3], int columns );
void main()
{
	do
	{
		redrawTable( table, NUMBER_OF_COLUMNS );
		cout << "Would you like to play a game of tic-tac-toe?(y/n)"
			<< endl;
		cin >> ans;
		system("cls");
		
		if (ans != char (121) && ans != char (110))
			cout << "Invalid iput, y or n." << endl;
			
		else if (ans == char (110))
			cout << "Thanks for playing!" << endl;
		break;
		
	}while (ans != char (110) && ans != char (121));

	if (ans == char (121))
		redrawTable( table, NUMBER_OF_COLUMNS );
		cout << "Player one, X's or O's?" << endl;
		cin >> play1;
		if (play1 == char (88))
			play2 = char (79);
		else if (play1 == char (79))
			play2 = char (88);
		system("cls");
		redrawTable( table, NUMBER_OF_COLUMNS );
		cout << "Player 1 is " << play1 << " and Player 2 is " << play2 << endl;
		cout << "X's make first move." << endl;
}

void redrawTable( int table[3][3], int col)
{
   for (row = 0; row < NUMBER_OF_ROWS; row++)
	{
		for (col = 0; col < NUMBER_OF_COLUMNS; col++)
			cout << table[row][col] << "______ ";
		cout << endl;
	}
}

what do i need to do so it doenst start the do loop if the user enters n?

Reply With Quote
  #9  
Old April 12th, 2007, 05:12 PM
Paul820 Paul820 is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2006
Location: United Kingdom
Posts: 428 Paul820 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 Days 7 h 55 m 4 sec
Reputation Power: 13
Where you have
Code:
if (ans == char (121))
enclose all the code below it with { and }, i'll show, it's easier
Code:
if (ans == char (121))
{ // <-- Here
	redrawTable( table, NUMBER_OF_COLUMNS );
	cout << "Player one, X's or O's?" << endl;
	cin >> play1;
	if (play1 == char (88))
		play2 = char (79);
	else if (play1 == char (79))
		play2 = char (88);
	system("cls");
	redrawTable( table, NUMBER_OF_COLUMNS );
	cout << "Player 1 is " << play1 << " and Player 2 is " << play2 << endl;
	cout << "X's make first move." << endl;
} // <--  And here


The compiler only accepts one statement after an if, if there are no { }'s, if you want the compiler to use more statement with the if ( or a while for that matter ), enclose the code in brackets like above.

Reply With Quote
  #10  
Old April 12th, 2007, 05:19 PM
MnkyScpe MnkyScpe is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2007
Posts: 16 MnkyScpe User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 h 19 m 55 sec
Reputation Power: 0
ahhh thanks that solved it! alos how do i make my board look more like a tic tac toe one? i mean right now it looks like this

1______ 2_______ 3______
4______ 5_______ 6______
7______ 8_______ 9______

and i want it to lok more like

__|__|__
__|__|__
__|__|__

Reply With Quote
  #11  
Old April 12th, 2007, 05:30 PM
Paul820 Paul820 is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2006
Location: United Kingdom
Posts: 428 Paul820 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 Days 7 h 55 m 4 sec
Reputation Power: 13
Make an array to hold it all. Looking at this

__|__|__
__|__|__
__|__|__

you need an array of 5 x 6 elements. So your players positions will have to change. The players options will be

0 2 4 for the top line
2 2 4 for the middle line
4 2 4 for the bottom line

not forgetting arrays start at zero

You will have to do a lot of checking to make sure the player is inside the grid and hasn't chosen a position where the | or __ is, and there isn't already an O or X there. It's a lot more work.

Reply With Quote
  #12  
Old April 12th, 2007, 05:36 PM
MnkyScpe MnkyScpe is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2007
Posts: 16 MnkyScpe User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 h 19 m 55 sec
Reputation Power: 0
well atm a lot more work isnt what im looking for lol... so ill stick with simple board i already got. Now when im saving a move to an area of the board for instance say they move in the upper right or 11 do i say table[1][1] = "X"? or is there another way of saving values to the specific quadrants?

Reply With Quote
  #13  
Old April 12th, 2007, 05:47 PM
Paul820 Paul820 is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2006
Location: United Kingdom
Posts: 428 Paul820 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 Days 7 h 55 m 4 sec
Reputation Power: 13
Yes, you assign a value like that, but you don't use " ( double quotes ) for a single character, use ' ( single quote ). Double quotes is seen as a string literal. You need the character literal. I have just noticed, your array is an int array. If you want to store characters in there, you will have to change it to a char array.

Reply With Quote
  #14  
Old April 12th, 2007, 05:59 PM
MnkyScpe MnkyScpe is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2007
Posts: 16 MnkyScpe User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 h 19 m 55 sec
Reputation Power: 0
what if i use the ascii value? like 79 = X i believe so id have it read in the char as an int but output as a char?

Reply With Quote
  #15  
Old April 12th, 2007, 06:06 PM
Paul820 Paul820 is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2006
Location: United Kingdom
Posts: 428 Paul820 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 Days 7 h 55 m 4 sec
Reputation Power: 13
It would work, but it's a lot of messing about don't you think? When you could just have an array of char. Or does it have to be int?

Reply With Quote
  #16  
Old April 12th, 2007, 07:29 PM
MnkyScpe MnkyScpe is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2007
Posts: 16 MnkyScpe User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 h 19 m 55 sec
Reputation Power: 0
it can be either but when i tried changing it form int to char i had to rework my board and im not sure how to do that. Im supposed to have the user enter coordinates in order to place the x or o so the middle would be 2 2. heres the exact assignment so maybe im interpreting it the wrong way and making it harder than it needs to be:


Write a program that will allow two people play a game of Tic-Tac-Toe. You need at least the following functions (more if you want).

1) A function that generates an array 3x3 of chars (for the board)

2) A function that places an X or O on the board in the players position. The player should be able to enter row x col for his position. So, if he enters 1 1 it would be upper left corner, 2 2 would be center, etc. Make sure this function checks to see if the position is already taken and allows the player to re-enter a position if so.

3) A function that checks to see if there is a winner. Let them keep playiong (alternating turns) until the is a winner or a tie. Display a message who the winner is and if there is a tie (the cat wins)

4) A function to print the board. Clear the screen, print the board and disply whose turn it is after each move.

5) After a game is over, prompt them and play another if desired. Keep track of the number of wins by each player and the number of cat wins and display those after they quit playing.

Reply With Quote
  #17  
Old April 13th, 2007, 09:26 AM
MnkyScpe MnkyScpe is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2007
Posts: 16 MnkyScpe User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 h 19 m 55 sec
Reputation Power: 0
i need some help... for some reason when i enter n when prompted to play the program still continues... heres the code:

Code:
#include <iostream>
#include <stdlib.h>
#include <iomanip>

using namespace std;

const int NUMBER_OF_ROWS = 3;
const int NUMBER_OF_COLUMNS = 3;

char play1;
char play2;
int move[9];
void drawboard(void);
char board[3][3];
int row;
int col;
char ans;
void main()
{
	drawboard();
	do
	{
		cout << "Would you like to play a game of tic tac toe? (y/n)" << endl;
		cin >> ans;
		if (ans == char (110))
		{
			system("cls");
			drawboard();
			cout << "Thanks for playing!" << endl;
			break;
		}
		if (ans != char (110) && ans != char (121))
		{
			system("cls");
			drawboard();
			cout << "Invalid input, y or n." << endl;
		}
	}while (ans != char (121));
	do
	{
		if (ans == char (121))
		{
			system ("cls");
			drawboard();
			cout << "Player 1, X's or O's?" << endl;
			cin >> play1;
		}
	if (play1 == char (88))
		play2 = char (79);
	else if (play1 == char (79))
		play2 = char (88);

	if (play1 == char (79))
	{	
		system ("cls");
		drawboard();
		cout << "Player 1 is " << play1 << "'s"<< " and Player 2 is " << play2 << "'s." << endl;
		cout << "X's, make the first move." << endl;
		cin >> move[0];
	}
	else if (play1 == char(88))
	{
		system ("cls");
		drawboard();
		cout << "Player 1 is " << play1 << "'s"<< " and Player 2 is " << play2 << "'s." << endl;
		cout << "X's, make the first move." << endl;
		cin >> move[0];
	}

		
		if (move[0] == 11)
		{
			board[0][0] = char (88);
			system ("cls");
			drawboard();
		}
		if (move[0] == 12)
		{
			board[0][1] = char (88);
			system ("cls");
			drawboard();
		}
		if (move[0] == 13)
		{
			board[0][2] = char (88);
			system ("cls");
			drawboard();
		}
		if (move[0] == 21)
		{
			board[1][0] = char (88);
			system ("cls");
			drawboard();
		}
		if (move[0] == 22)
		{
			board[1][1] = char (88);
			system ("cls");
			drawboard();
		}
		if (move[0] == 23)
		{
			board[1][2] = char (88);
			system ("cls");
			drawboard();
		}
		if (move[0] == 31)
		{
			board[2][0] = char (88);
			system ("cls");
			drawboard();
		}
		if (move[0] == 32)
		{
			board[2][1] = char (88);
			system ("cls");
			drawboard();
		}
		if (move[0] == 33)
		{
			board[2][2] = char (88);
			system ("cls");
			drawboard();
		}
		do
		{
		cout << "O's, your move." << endl;
		cin >> move[1];
			system("cls");
			drawboard();			
		if (move[1] == move[0])
			cout << "Cant move there." << endl;

			
		}while (move[1] == move[0]);
		
		if (move[1] == 11)
		{
			board[0][0] = char (79);
			system ("cls");
			drawboard();
		}
		if (move[1] == 12)
		{
			board[0][1] = char (79);
			system ("cls");
			drawboard();
		}
		if (move[1] == 13)
		{
			board[0][2] = char (79);
			system ("cls");
			drawboard();
		}
		if (move[1] == 21)
		{
			board[1][0] = char (79);
			system ("cls");
			drawboard();
		}
		if (move[1] == 22)
		{
			board[1][1] = char (79);
			system ("cls");
			drawboard();
		}
		if (move[1] == 23)
		{
			board[1][2] = char (79);
			system ("cls");
			drawboard();
		}
		if (move[1] == 31)
		{
			board[2][0] = char (79);
			system ("cls");
			drawboard();
		}
		if (move[1] == 32)
		{
			board[2][1] = char (79);
			system ("cls");
			drawboard();
		}
		if (move[1] == 33)
		{
			board[2][2] = char (79);
			system ("cls");
			drawboard();
		}
		do
		{
		cout << "X's, your move." << endl;
		cin >> move[2];
			system("cls");
			drawboard();			
		if (move[2] == move[1] || move[2] == move[0])
			cout << "Cant move there." << endl;
			
		}while (move[2] == move[1] || move[2] == move[0]);
		if (move[2] == 11)
		{
			board[0][0] = char (88);
			system ("cls");
			drawboard();
		}
		if (move[2] == 12)
		{
			board[0][1] = char (88);
			system ("cls");
			drawboard();
		}
		if (move[2] == 13)
		{
			board[0][2] = char (88);
			system ("cls");
			drawboard();
		}
		if (move[2] == 21)
		{
			board[1][0] = char (88);
			system ("cls");
			drawboard();
		}
		if (move[2] == 22)
		{
			board[1][1] = char (88);
			system ("cls");
			drawboard();
		}
		if (move[2] == 23)
		{
			board[1][2] = char (88);
			system ("cls");
			drawboard();
		}
		if (move[2] == 31)
		{
			board[2][0] = char (88);
			system ("cls");
			drawboard();
		}
		if (move[2] == 32)
		{
			board[2][1] = char (88);
			system ("cls");
			drawboard();
		}
		if (move[2] == 33)
		{
			board[2][2] = char (88);
			system ("cls");
			drawboard();
		}
		do
		{
		cout << "O's, your move." << endl;
		cin >> move[3];
			system("cls");
			drawboard();			
		if (move[3] == move[2] || move[3] == move[1] || move[3] == move[0])
			cout << "Cant move there." << endl;
			
		}while (move[3] == move[2] || move[3] == move[1] || move[3] == move[0]);
		if (move[3] == 11)
		{
			board[0][0] = char (79);
			system ("cls");
			drawboard();
		}
		if (move[3] == 12)
		{
			board[0][1] = char (79);
			system ("cls");
			drawboard();
		}
		if (move[3] == 13)
		{
			board[0][2] = char (79);
			system ("cls");
			drawboard();
		}
		if (move[3] == 21)
		{
			board[1][0] = char (79);
			system ("cls");
			drawboard();
		}
		if (move[3] == 22)
		{
			board[1][1] = char (79);
			system ("cls");
			drawboard();
		}
		if (move[3] == 23)
		{
			board[1][2] = char (79);
			system ("cls");
			drawboard();
		}
		if (move[3] == 31)
		{
			board[2][0] = char (79);
			system ("cls");
			drawboard();
		}
		if (move[3] == 32)
		{
			board[2][1] = char (79);
			system ("cls");
			drawboard();
		}
		if (move[3] == 33)
		{
			board[2][2] = char (79);
			system ("cls");
			drawboard();
		}
		do
		{
		cout << "X's, your move." << endl;
		cin >> move[4];
			system("cls");
			drawboard();			
		if (move[4] == move[3] || move[4] == move[2] || move[4] == move[1] || move[4] == move[0])
			cout << "Cant move there." << endl;
			
		}while (move[4] == move[3] || move[4] == move[2] || move[4] == move[1] || move[4] == move[0]);
		if (move[4] == 11)
		{
			board[0][0] = char (88);
			system ("cls");
			drawboard();
		}
		if (move[4] == 12)
		{
			board[0][1] = char (88);
			system ("cls");
			drawboard();
		}
		if (move[4] == 13)
		{
			board[0][2] = char (88);
			system ("cls");
			drawboard();
		}
		if (move[4] == 21)
		{
			board[1][0] = char (88);
			system ("cls");
			drawboard();
		}
		if (move[4] == 22)
		{
			board[1][1] = char (88);
			system ("cls");
			drawboard();
		}
		if (move[4] == 23)
		{
			board[1][2] = char (88);
			system ("cls");
			drawboard();
		}
		if (move[4] == 31)
		{
			board[2][0] = char (88);
			system ("cls");
			drawboard();
		}
		if (move[4] == 32)
		{
			board[2][1] = char (88);
			system ("cls");
			drawboard();
		}
		if (move[4] == 33)
		{
			board[2][2] = char (88);
			system ("cls");
			drawboard();
		}
}while (ans == char(121));

i have a do while loop but somehow the program enters it even though it doesnt meet the rquirements.

Reply With Quote
  #18  
Old April 13th, 2007, 10:43 AM
Paul820 Paul820 is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2006
Location: United Kingdom
Posts: 428 Paul820 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 Days 7 h 55 m 4 sec
Reputation Power: 13
Looking at your assignment, it does say a char array. I see you have changed it to a char now so things will be easier to do. Also your assignment says you need a certain amount of function to complete the task ( more if you want ). Instead of all those if statements you can just use a function for player one and a function for player two to put each of their positions in the array.

Pass the array to each of the function along with the position that the player has chosen along with the character of player one or player two:

Code:
void updatePlayerOnePos( char table[][3], size, int x, int y, char pl1 );
void updatePlayerTwoPos( char table[][3], size, int x, int y, char pl2 );


Here you are passing the array, the players row position, the players column position, and either the players X or O character.
Now when you ask for the position for player 1 or player 2, pass the position the player chose

Code:
updatePlayerOnePos( table, NUMBER_OF_COLUMNS, x, y, play1 );
updatePlayerTwoPos( table, NUMBER_OF_COLUMNS, x, y, play2 );


Inside each function:

table[x][y] = pl1 for player one
and
table[x][y] = pl2 for player two
Not forgetting there are two seperate functions
Just a suggestion. Anyway, look at the second do...while in main. There is no while.

Reply With Quote
  #19  
Old April 13th, 2007, 11:12 AM
Paul820 Paul820 is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2006
Location: United Kingdom
Posts: 428 Paul820 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 Days 7 h 55 m 4 sec
Reputation Power: 13
If you don't understand what i mean, i have done a working example to show you. If you want to see it, just ask. But please don't just copy and paste it. Go through it with a debugger and follow each function call and what is being passed etc...

Reply With Quote
  #20  
Old April 13th, 2007, 11:39 AM
MnkyScpe MnkyScpe is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2007
Posts: 16 MnkyScpe User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 h 19 m 55 sec
Reputation Power: 0
ok im sorta lost on how to find what parameters to use in functions like the one u posted on updatePlayer2Pos etc. Also id like to see your code to see if maybe it makes things clearer.

Reply With Quote
  #21  
Old April 13th, 2007, 11:55 AM
Paul820 Paul820 is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2006
Location: United Kingdom
Posts: 428 Paul820 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 Days 7 h 55 m 4 sec
Reputation Power: 13
Ok here you go, it's commented:
Code:
#include <cstddef>
#include <iostream>

void updatePlayerOnePos( char board[][3], size_t size, int x, int y, char pl1 );
void updatePlayerTwoPos( char board[][3], size_t size, int x, int y, char pl2 );
void showBoard( char board[][3], size_t size );

int main()
{
   const size_t ROWS = 3;
   const size_t COLS = 3;
   char table[ROWS][COLS] = { { '*', '*', '*' },
                              { '*', '*', '*' },
                              { '*', '*', '*' } };

   char player1 = 'X';
   char player2 = 'O';

   int rowPosition;
   int colPosition;

   // This would all go in a loop
   // Assume this is player one
   std::cout << "Player 1 enter x and y coordinates: ";
   std::cin >> rowPosition >> colPosition;

   // Pass the board, how many rows the board has, player x position, player y
   // position, and the character for the player
   updatePlayerOnePos( table, ROWS, rowPosition, colPosition, player1 );
   showBoard( table, ROWS );

   // Assume this is player two
   std::cout << "Player 2 enter x and y coordinates: ";
   std::cin >> rowPosition >> colPosition;

   // Pass the board, how many rows the board has, player x position, player y
   // position, and the character for the player
   updatePlayerTwoPos( table, ROWS, rowPosition, colPosition, player2 );
   showBoard( table, ROWS );

   // You would need some error checking for the positions, and some code
   // to check for a winner before putting the character in the array incase
   // someones valid move got overwritten

   return 0;
}

void updatePlayerOnePos( char board[][3], size_t size, int x, int y, char pl1 )
{
   // Assign the player one's character to the board with the x and y
   // variables passed from main
   board[x][y] = pl1;
}

void updatePlayerTwoPos( char board[][3], size_t size, int x, int y, char pl2 )
{
   // Assign the player two's character to the board with the x and y
   // variables passed from main
   board[x][y] = pl2;
}

void showBoard( char board[][3], size_t size )
{
   // Just shows the board
   for ( size_t r = 0; r < size; r++ )
   {
      for ( size_t c = 0; c < size; c++ )
      {
         std::cout << board[r][c];
      }

      std::cout << std::endl;
   }
}


If you havn't covered size_t yet, just change them to int's. And ignore the std::'s, it's just the way i prefer to code. Hope it gives you some helpful hints.

Reply With Quote
  #22  
Old April 13th, 2007, 12:29 PM
MnkyScpe MnkyScpe is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2007
Posts: 16 MnkyScpe User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 h 19 m 55 sec
Reputation Power: 0
i appreciate all your help but ur program confuses me heh... i got mine working how i want it i just need to figure out why after it finds a winner it continues the loop...
Code:
#include <iostream>
#include <stdlib.h>
#include <iomanip>

using namespace std;

const int NUMBER_OF_ROWS = 3;
const int NUMBER_OF_COLUMNS = 3;

char play1;
char play2;
int move[8];
void checkwin(void);
void drawboard(void);
char board[3][3];
int row;
int col;
void main()
{


		drawboard();		
		system ("cls");
		drawboard();
		cout << "Player 1, X's or O's?" << endl;
		cin >> play1;
	if (play1 == char (88))
		play2 = char (79);
	else if (play1 == char (79))
		play2 = char (88);

	if (play1 == char (79))
	{	
		system ("cls");
		drawboard();
		cout << "X's, make the first move." << endl;
		cin >> move[0];
	}
	else if (play1 == char(88))
	{
		system ("cls");
		drawboard();
		cout << "X's, make the first move." << endl;
		cin >> move[0];
	}
		if (move[0] == 11)
		{
			board[0][0] = char (88);
			system ("cls");
			drawboard();
		}
		if (move[0] == 12)
		{
			board[0][1] = char (88);
			system ("cls");
			drawboard();
		}
		if (move[0] == 13)
		{
			board[0][2] = char (88);
			system ("cls");
			drawboard();
		}
		if (move[0] == 21)
		{
			board[1][0] = char (88);
			system ("cls");
			drawboard();
		}
		if (move[0] == 22)
		{
			board[1][1] = char (88);
			system ("cls");
			drawboard();
		}
		if (move[0] == 23)
		{
			board[1][2] = char (88);
			system ("cls");
			drawboard();
		}
		if (move[0] == 31)
		{
			board[2][0] = char (88);
			system ("cls");
			drawboard();
		}
		if (move[0] == 32)
		{
			board[2][1] = char (88);
			system ("cls");
			drawboard();
		}
		if (move[0] == 33)
		{
			board[2][2] = char (88);
			system ("cls");
			drawboard();
		}
		do
		{
		cout << "O's, your move." << endl;
		cin >> move[1];
			system("cls");
			drawboard();			
		if (move[1] == move[0])
			cout << "Cant move there." << endl;

			
		}while (move[1] == move[0]);
		
		if (move[1] == 11)
		{
			board[0][0] = char (79);
			system ("cls");
			drawboard();
		}
		if (move[1] == 12)
		{
			board[0][1] = char (79);
			system ("cls");
			drawboard();
		}
		if (move[1] == 13)
		{
			board[0][2] = char (79);
			system ("cls");
			drawboard();
		}
		if (move[1] == 21)
		{
			board[1][0] = char (79);
			system ("cls");
			drawboard();
		}
		if (move[1] == 22)
		{
			board[1][1] = char (79);
			system ("cls");
			drawboard();
		}
		if (move[1] == 23)
		{
			board[1][2] = char (79);
			system ("cls");
			drawboard();
		}
		if (move[1] == 31)
		{
			board[2][0] = char (79);
			system ("cls");
			drawboard();
		}
		if (move[1] == 32)
		{
			board[2][1] = char (79);
			system ("cls");
			drawboard();
		}
		if (move[1] == 33)
		{
			board[2][2] = char (79);
			system ("cls");
			drawboard();
		}
		do
		{
		cout << "X's, your move." << endl;
		cin >> move[2];
			system("cls");
			drawboard();			
		if (move[2] == move[1] || move[2] == move[0])
			cout << "Cant move there." << endl;
			
		}while (move[2] == move[1] || move[2] == move[0]);
		if (move[2] == 11)
		{
			board[0][0] = char (88);
			system ("cls");
			drawboard();
		}
		if (move[2] == 12)
		{
			board[0][1] = char (88);
			system ("cls");
			drawboard();
		}
		if (move[2] == 13)
		{
			board[0][2] = char (88);
			system ("cls");
			drawboard();
		}
		if (move[2] == 21)
		{
			board[1][0] = char (88);
			system ("cls");
			drawboard();
		}
		if (move[2] == 22)
		{
			board[1][1] = char (88);
			system ("cls");
			drawboard();
		}
		if (move[2] == 23)
		{
			board[1][2] = char (88);
			system ("cls");
			drawboard();
		}
		if (move[2] == 31)
		{
			board[2][0] = char (88);
			system ("cls");
			drawboard();
		}
		if (move[2] == 32)
		{
			board[2][1] = char (88);
			system ("cls");
			drawboard();
		}
		if (move[2] == 33)
		{
			board[2][2] = char (88);
			system ("cls");
			drawboard();
		}
		do
		{
		cout << "O's, your move." << endl;
		cin >> move[3];
			system("cls");
			drawboard();			
		if (move[3] == move[2] || move[3] == move[1] || move[3] == move[0])
			cout << "Cant move there." << endl;
			
		}while (move[3] == move[2] || move[3] == move[1] || move[3] == move[0]);
		if (move[3] == 11)
		{
			board[0][0] = char (79);
			system ("cls");
			drawboard();
		}
		if (move[3] == 12)
		{
			board[0][1] = char (79);
			system ("cls");
			drawboard();
		}
		if (move[3] == 13)
		{
			board[0][2] = char (79);
			system ("cls");
			drawboard();
		}
		if (move[3] == 21)
		{
			board[1][0] = char (79);
			system ("cls");
			drawboard();
		}
		if (move[3] == 22)
		{
			board[1][1] = char (79);
			system ("cls");
			drawboard();
		}
		if (move[3] == 23)
		{
			board[1][2] = char (79);
			system ("cls");
			drawboard();
		}
		if (move[3] == 31)
		{
			board[2][0] = char (79);
			system ("cls");
			drawboard();
		}
		if (move[3] == 32)
		{
			board[2][1] = char (79);
			system ("cls");
			drawboard();
		}
		if (move[3] == 33)
		{
			board[2][2] = char (79);
			system ("cls");
			drawboard();
		}
		do
		{
		cout << "X's, your move." << endl;
		cin >> move[4];
			system("cls");
			drawboard();			
		if (move[4] == move[3] || move[4] == move[2] || move[4] == move[1] || move[4] == move[0])
			cout << "Cant move there." << endl;
			
		}while (move[4] == move[3] || move[4] == move[2] || move[4] == move[1] || move[4] == move[0]);
		if (move[4] == 11)
		{
			board[0][0] = char (88);
			system ("cls");
			drawboard();
		}
		if (move[4] == 12)
		{
			board[0][1] = char (88);
			system ("cls");
			drawboard();
		}
		if (move[4] == 13)
		{
			board[0][2] = char (88);
			system ("cls");
			drawboard();
		}
		if (move[4] == 21)
		{
			board[1][0] = char (88);
			system ("cls");
			drawboard();
		}
		if (move[4] == 22)
		{
			board[1][1] = char (88);
			system ("cls");
			drawboard();
		}
		if (move[4] == 23)
		{
			board[1][2] = char (88);
			system ("cls");
			drawboard();
		}
		if (move[4] == 31)
		{
			board[2][0] = char (88);
			system ("cls");
			drawboard();
		}
		if (move[4] == 32)
		{
			board[2][1] = char (88);
			system ("cls");
			drawboard();
		}
		if (move[4] == 33)
		{
			board[2][2] = char (88);
			system ("cls");
			drawboard();
		}
		checkwin();
		do
		{
		cout << "O's, your move." << endl;
		cin >> move[5];
			system("cls");
			drawboard();			
		if (move[5] == move[4] || move[5] == move[3] || move[5] == move[2] || move[5] == move[1]
			|| move[5] == move[0])
			cout << "Cant move there." << endl;
			
		}while (move[5] == move[4] || move[5] == move[3] || move[5] == move[2] || move[5] == move[1]
			|| move[5] == move[0]);
		if (move[5] == 11)
		{
			board[0][0] = char (79);
			system ("cls");
			drawboard();
		}
		if (move[5] == 12)
		{
			board[0][1] = char (79);
			system ("cls");
			drawboard();
		}
		if (move[5] == 13)
		{
			board[0][2] = char (79);
			system ("cls");
			drawboard();
		}
		if (move[5] == 21)
		{
			board[1][0] = char (79);
			system ("cls");
			drawboard();
		}
		if (move[5] == 22)
		{
			board[1][1] = char (79);
			system ("cls");
			drawboard();
		}
		if (move[5] == 23)
		{
			board[1][2] = char (79);
			system ("cls");
			drawboard();
		}
		if (move[5] == 31)
		{
			board[2][0] = char (79);
			system ("cls");
			drawboard();
		}
		if (move[5] == 32)
		{
			board[2][1] = char (79);
			system ("cls");
			drawboard();
		}
		if (move[5] == 33)
		{
			board[2][2] = char (79);
			system ("cls");
			drawboard();
		}
		checkwin();
		do
		{
		cout << "X's, your move." << endl;
		cin >> move[6];
			system("cls");
			drawboard();			
		if (move[6] == move[5] || move[6] == move[4] || move[6] == move[3] || move[6] == move[2])
			cout << "Cant move there." << endl;
		if (move[6]== move[1] || move[6] == move[0])
			cout << "Cant move there." << endl;
			
		}while (move[6] == move[5] || move[6] == move[4] || move[6] == move[3] || move[6] == move[2]|| move[6]== move[1] || move[6] == move[0]);
			
		if (move[6] == 11)
		{
			board[0][0] = char (88);
			system ("cls");
			drawboard();
		}
		if (move[6] == 12)
		{
			board[0][1] = char (88);
			system ("cls");
			drawboard();
		}
		if (move[6] == 13)
		{
			board[0][2] = char (88);
			system ("cls");
			drawboard();
		}
		if (move[6] == 21)
		{
			board[1][0] = char (88);
			system ("cls");
			drawboard();
		}
		if (move[6] == 22)
		{
			board[1][1] = char (88);
			system ("cls");
			drawboard();
		}
		if (move[6] == 23)
		{
			board[1][2] = char (88);
			system ("cls");
			drawboard();
		}
		if (move[6] == 31)
		{
			board[2][0] = char (88);
			system ("cls");
			drawboard();
		}
		if (move[6] == 32)
		{
			board[2][1] = char (88);
			system ("cls");
			drawboard();
		}
		if (move[6] == 33)
		{
			board[2][2] = char (88);
			system ("cls");
			drawboard();
		}
		checkwin();
		do
		{
		cout << "O's, your move." << endl;
		cin >> move[7];
			system("cls");
			drawboard();			
		if (move[7] == move[6] || move[7] == move[5] || move[7] == move[4] || move[7] == move[3])
			cout << "Cant move there." << endl;
		if (move[7]== move[2] || move[7] == move[1] || move[7] == move[0])
			cout << "Cant move there." << endl;
			
		}while (move[7] == move[6] || move[7] == move[5] || move[7] == move[4] || move[7] == move[3]|| move[7]== move[2] || move[7] == move[1] || move[7] == move[0]);
			
		if (move[7] == 11)
		{
			board[0][0] = char (79);
			system ("cls");
			drawboard();
		}
		if (move[7] == 12)
		{
			board[0][1] = char (79);
			system ("cls");
			drawboard();
		}
		if (move[7] == 13)
		{
			board[0][2] = char (79);
			system ("cls");
			drawboard();
		}
		if (move[7] == 21)
		{
			board[1][0] = char (79);
			system ("cls");
			drawboard();
		}
		if (move[7] == 22)
		{
			board[1][1] = char (79);
			system ("cls");
			drawboard();
		}
		if (move[7] == 23)
		{
			board[1][2] = char (79);
			system ("cls");
			drawboard();
		}
		if (move[7] == 31)
		{
			board[2][0] = char (79);
			system ("cls");
			drawboard();
		}
		if (move[7] == 32)
		{
			board[2][1] = char (79);
			system ("cls");
			drawboard();
		}
		if (move[7] == 33)
		{
			board[2][2] = char (79);
			system ("cls");
			drawboard();
		}
		checkwin();
		do
		{
		cout << "X's, your move." << endl;
		cin >> move[8];
			system("cls");
			drawboard();			
		if (move[8] == move[7] || move[8] == move[6] || move[8] == move[5] || move[8] == move[4])
			cout << "Cant move there." << endl;
		if (move[8]== move[3] || move[8] == move[2] || move[8] == move[1] || move[8] == move[0])
			cout << "Cant move there." << endl;
			
		}while (move[8] == move[7] || move[8] == move[6] || move[8] == move[5] || move[8] == move[4]|| move[8]== move[3] || move[8] == move[2] || move[8] == move[1] || move[8] == move[0]);
			
		if (move[8] == 11)
		{
			board[0][0] = char (88);
			system ("cls");
			drawboard();
		}
		if (move[8] == 12)
		{
			board[0][1] = char (88);
			system ("cls");
			drawboard();
		}
		if (move[8] == 13)
		{
			board[0][2] = char (88);
			system ("cls");
			drawboard();
		}
		if (move[8] == 21)
		{
			board[1][0] = char (88);
			system ("cls");
			drawboard();
		}
		if (move[8] == 22)
		{
			board[1][1] = char (88);
			system ("cls");
			drawboard();
		}
		if (move[8] == 23)
		{
			board[1][2] = char (88);
			system ("cls");
			drawboard();
		}
		if (move[8] == 31)
		{
			board[2][0] = char (88);
			system ("cls");
			drawboard();
		}
		if (move[8] == 32)
		{
			board[2][1] = char (88);
			system ("cls");
			drawboard();
		}
		if (move[8] == 33)
		{
			board[2][2] = char (88);
			system ("cls");
			drawboard();
		}
		checkwin();


			


}


void drawboard(void)
{
	cout << "Welcome to Tic Tac Toe!" << endl;;
	for(row = 0; row < NUMBER_OF_ROWS; row++)
	{
		for(col = 0; col < NUMBER_OF_COLUMNS; col++)
			{
			cout << board[row][col] << " |" << setw(2);
			}
		cout << endl << "-----------" << endl;
	}
}

void checkwin(void)
{
	//verticle check for x.
	if (board[0][0] == char(88) && board[1][0] == char(88) && board[2][0] == char(88))
		cout << "X's win!" << endl;
	if (board[0][1] == char(88) && board[1][1] == char(88) && board[2][1] == char(88))
		cout << "X's win!" << endl;
	if (board[0][2] == char(88) && board[1][2] == char(88) && board[2][2] == char(88))
		cout << "X's win!" << endl;
	//horizontal check for x.
	if (board[0][0] == char(88) && board[0][1] == char(88) && board[0][2] == char(88))
		cout << "X's win!" << endl;
	if (board[1][0] == char(88) && board[1][1] == char(88) && board[1][2] == char(88))
		cout << "X's win!" << endl;
	if (board[2][0] == char(88) && board[2][1] == char(88) && board[2][2] == char(88))
		cout << "X's win!" << endl;
	//diag check for x.
	if (board[0][0] == char(88) && board[1][1] == char(88) && board[2][2] == char(88))
		cout << "X's win!" << endl;
	if (board[2][0] == char(88) && board[1][1] == char(88) && board[0][2] == char(88))
		cout << "X's win!" << endl;

	//verticle check for o.
	if (board[0][0] == char(79) && board[1][0] == char(79) && board[2][0] == char(79))
		cout << "O's win!" << endl;
	if (board[0][1] == char(79) && board[1][1] == char(79) && board[2][1] == char(79))
		cout << "O's win!" << endl;
	if (board[0][2] == char(79) && board[1][2] == char(79) && board[2][2] == char(79))
		cout << "O's win!" << endl;
	//horizontal check for o.
	if (board[0][0] == char(79) && board[0][1] == char(79) && board[0][2] == char(79))
		cout << "O's win!" << endl;
	if (board[1][0] == char(79) && board[1][1] == char(79) && board[1][2] == char(79))
		cout << "O's win!" << endl;
	if (board[2][0] == char(79) && board[2][1] == char(79) && board[2][2] == char(79))
		cout << "O's win!" << endl;
	//diag check for o.
	if (board[0][0] == char(79) && board[1][1] == char(79) && board[2][2] == char(79))
		cout << "O's win!" << endl;
	if (board[2][0] == char(79) && board[1][1] == char(79) && board[0][2] == char(79))
		cout << "O's win!" << endl;
}

i also need to find a way to loop it back ot the start if they want to play another game and how to score for a cats game.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Tic tac toe help


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap