473,385 Members | 1,893 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 6958
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

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

Similar topics

19
by: Dave | last post by:
Hi, I have done some research, trying to Clear The Screen in java code. The first option was the obv: system.out.print("\n\n\n\n\n\n\n\n\n\n\n\n"); then i heard about this method:...
1
by: Devrim Erdem | last post by:
Hello, I am on win32 with python2.2. My python added C++ app is an OpenGL application so there is no real GUI code around it. I have built in the python interpreter which works very great. I...
4
by: porous | last post by:
hi i would like to develop applications for windows in c++ , but i dont want to do any gui .i just want the program to be in console mode . but i would like to access the widows api . can any...
1
by: Eitan | last post by:
Hello, I am using Visual Studio.net 2003. I am trying to find how to create Win32 DLL & Win32 Static Library. When going through the wizard it does not show me the option for these two project...
3
by: Dr. Erhard Moenige | last post by:
Hi all C-Programmers everywhere ! I 've been learning programming c in console-applications for 1 year and now I'm tired of seeing this boring console-application in my programms. Because of...
2
by: SheetalGandhi24 | last post by:
Hi I need to use the System.IO namespace in the Win32 console application. Any clues for doing the same? Basically, I need to load the files of a particular folder in an array. I need to do...
0
by: jbenezech | last post by:
Hi all , I have a perl/java app running under Win32. The application consists of a perl service (Win32::Daemon) and of java classes. The perl service calls every xx hours java classes to perform...
4
by: Peter Nimmo | last post by:
Hi, I am writting a windows application that I want to be able to act as if it where a Console application in certain circumstances, such as error logging. Whilst I have nearly got it, it...
2
by: Frank Moyles | last post by:
Hi, I want to use SciPy library. I am using W2k, and ActiveState Python 2.5. I have succesfully numpy, but when I run the scipy-0.6.0.win32-py2.5.exe (from the downloads section on the SciPy...
14
by: =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?= | last post by:
As far as I know, the C Standard has no mention of multi-threaded programming; it has no mention of how to achieve multi-threaded programming, nor does it mention whether the language or its...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.