468,496 Members | 1,795 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,496 developers. It's quick & easy.

C++ - Programming Tic Tac Toe Win32 Console Application

Hey folks,

Thought I could use some help with this. I have the basic foundation around the app, but cannot seem to get things working correctly.

Can someone have a look at the code below and help me to tweak it to get it working correctly?


Thanks in advance,

VPascuzzi.

Expand|Select|Wrap|Line Numbers
  1. // TicTacToe.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <cctype>
  6. #include <conio.h>
  7. #include <iostream>
  8. #include <iomanip>
  9. //#include <stdlib.h>
  10. using std::cin;
  11. using std::endl;
  12.  
  13. // Function Prototypes
  14. int Display_Grid( int* Cells );  // Displays tic tac toe grid; refreshed every turn
  15. int Check_Game_State( int* Cells );  // Tracks game status to determine WIN, LOSE, TIE, CONTINUE
  16. int Machine_Move( int* Cells );  // AI computer move
  17. int Open_Spots( int* Cells );  // Function to display open cells
  18.  
  19. int _tmain(int argc, _TCHAR* argv[])
  20. {
  21.     int Player1_Move = NULL; // Player1 move temporary storage
  22.     int Cells[9] = { NULL };  // Array for storing cell contents
  23.     int Open_Cells[9] = { NULL };  // Array for storing list of open cells
  24.     int Game_State_Flag = 0;  //{ "CONTINUE", "WIN", "LOSE", "DRAW" );
  25.  
  26.     for ( int Move_Counter = 0; Move_Counter < 9; Move_Counter++ ){
  27.         Display_Grid( Cells );
  28.         Open_Spots( Open_Cells );
  29.  
  30.         printf( "\nPlayer please select a number from 1 - 9: " );
  31.         cin >> Player1_Move;
  32.  
  33.         Open_Cells[Player1_Move-1] = 1;
  34.         Cells[Player1_Move-1] = 79;
  35.  
  36.         Open_Cells[Machine_Move( Cells )];        // CPU makes move depending on Cells[] values
  37.  
  38.         system( "CLS" );
  39.     }
  40.     return 0;
  41. }
  42.  
  43. // Function Definitions
  44. int Display_Grid( int* Cells ){
  45.         printf( " %c | %c | %c \n", Cells[0], Cells[1], Cells[2]);
  46.         printf( "---+---+---\n");
  47.         printf( " %c | %c | %c \n", Cells[3], Cells[4], Cells[5]);
  48.         printf( "---+---+---\n");
  49.         printf( " %c | %c | %c \n", Cells[6], Cells[7], Cells[8]);
  50.  
  51.     return 0;
  52. } // end Display_Grid function definition
  53.  
  54. int Check_Game_State( int* Cells ){
  55.  
  56.     return 0;
  57. }// end Check_Game_State function definition
  58.  
  59. int Machine_Move( int* Cells ){
  60.     for ( int i = 0; i < 9; i++ ){
  61.         for ( int j = 1; j <= 9; j++ ){
  62.             if( ( Cells[i+j] == NULL ) && ( Cells[i] + Cells[j] == 158 ) )
  63.                 Cells[i+j] = 88;
  64.         }
  65.     }
  66.     return *Cells;
  67. } // end Machine_Move function definition
  68.  
  69. /* Fix this function
  70.    It currently checks the Cells array incorrectly.*/
  71. int Open_Spots( int* Cells ){
  72.     printf( "Open spots remaining:\n" );
  73.     for ( int i = 1; i <= 9; i++ ){
  74.         if ( Cells[i-1] == NULL )
  75.             printf( " %d ", i );
  76.     }
  77.     return *Cells;
  78. }
  79.  
Jan 17 '08 #1
3 6646
Laharl
849 Expert 512MB
Try returning a pointer to the array, with &Cells. The return type would be int**.

There's also no need to have these int functions (like display) return 0 when they can be void, especially since there is no exception handling to take advantage of return states.

Also, what's up with all these nonstandard headers? I don't know 'stdafx.h', but conio has a pretty bad reputation.
Jan 17 '08 #2
Savage
1,764 Expert 1GB
Expand|Select|Wrap|Line Numbers
  1. int Player1_Move = NULL; // Player1 move temporary storage
Can you explain me what is this supposed to do?
Also please explain your problem in greater detail.By telling I'm having a problem and not specifying what it is,make as guess things,instead of being able to accurately help you.It's recommended to read our posting guidelines,especially the part where it's described how to post accurate question.
Jan 17 '08 #3
Studlyami
464 Expert 256MB
What exactly is your problem? Heres a few things i see.
for ( int Move_Counter = 0; Move_Counter < 9; Move_Counter++ ){
Display_Grid( Cells );
Open_Spots( Open_Cells );
int Check_Game_State( int* Cells ) {

return 0;
}// end Check_Game_State function definition
The game can end before 9 moves. So you need to have an array that checks all rows, then all columns (straight across, then straight drown) to see if the cells equal x or o. Then you need to check the diagonals to see if any of the two diagonals is equal to 3 x's or o's. This needs to go in the Check_Game_State function. That means you would need to change you main loop to a do while loop. I would create a boolean value to keep track if the game is finished or not (used in the loop i described above). The Check_Game_State function should return the boolean value used in the loop i described above.

int Cells[9] = { NULL }; // Array for storing cell contents
If your storing the cells and setting them to NULL then if a Cell == NULL the cell is open. The open cell array is not needed.

Also I'm not quite sure what your logic is behind the machine move function mind explaining that one a little bit better? Anyways i hope this helps. If any of this is unclear let me know.
Jan 17 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

19 posts views Thread by Dave | last post: by
1 post views Thread by Devrim Erdem | last post: by
4 posts views Thread by porous | last post: by
1 post views Thread by Eitan | last post: by
3 posts views Thread by Dr. Erhard Moenige | last post: by
2 posts views Thread by Frank Moyles | last post: by
14 posts views Thread by =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?= | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.