Here is my tic tac toe simulation game, is there any way to rewrite this code so that it isn't so large without using arrays? Any help is greatly appreciated.
proj3.c
Here is my tic tac toe simulation game, is there any way to rewrite this code so that it isn't so large without using arrays? Any help is greatly appreciated.
proj3.c
Last edited by SaraDawkins; 03-04-2012 at 01:58 PM.
Yes, you should do two things to improve your code:
- Dont use magic numbers, instead, use constant #defines for the win status
- Use arrays for the board. Typically, you'll want a 3x3 array of 1-byte values.
Code:char board[3][3]; /* or */ int i; char **board = malloc(3 * sizeof(char *)); for (i = 0; i < 3; i++) board[i] = malloc(3 * sizeof(char));
Thank you for the suggestions, however I have been specifically ordered not to use arrays. I am curious however as to what you mean by, "don't use magic numbers, use constant #defines for the win status." Can you please give me an example of what this means?
In your function findWin, all of the values for pXwin and pOwin are called "magic numbers". That means that they're random numbers with no meaning outside your code. To fix that, use #defines or enums:
Or, you could just print the results immediately and combine findWin and printWin.Code:enum win_codes { TOP_ROW, MIDDLE_ROW, BOTTOM_ROW, /* ..... */ LEFT_DIAGONAL }; /* ... */ if (topR == 1 && topC == 1 && topL == 1) { *pXwin = TOP_ROW; }
--------
> I have been specifically ordered not to use arrays
Wtf? What kind of assignment is that? Can you use structs? Like this:
Code:struct row { char y1, y2, y3; }; struct board { struct row x1, x2, x3; }; /* ... */ struct board board; board.x1.y1 = 'x'; board.x2.y3 = 'o'; /* ... etc */
Thanks for the clarification. Yeah, tell me about it... I'm not allowed to use structs either, it is frustrating.
Last edited by SaraDawkins; 03-04-2012 at 02:34 PM.
How would I use #defines instead of enums in that situation?
I suppose the answer they're looking for is the one using bits.
For example, you can represent all the 'X' positions with
int xboard = 0;
...
xboard |= (1 << moveposition);
where moveposition is from 0 to 8
Checking for winning lines is really simple.
Eg.
if ( (xboard & 0x7) == 0x7 ) // first row
You can test the whole board with 8 such statements.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
I could, but it would be pretty much handing you the answer on a plate.
And to bounce back an answer in less than 3 minutes shows some lack of initiative.
> (I am new to coding)
You've been here for 5 months, you should have come across bitwise operators by now.
Now that you know you can represent a board in only 9 bits, spend some time drawing bit patterns out on paper until you figure it out.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
I wish I knew what this meant. Sorry if I've offended you somehow.
Read these links
> Tutorials - Bitwise Operators and Bit Manipulations in C and C++ - Cprogramming.com
> Bitwise Operations in C
> Bitwise Operators in C
Also, @salem, how did I not think of that? o_o
Why are you assigning the spaces values of 0 and 1 and then changing to X and O?
Just do this:
*pTopL = (rand() % 2) ? 'X' : 'O';
This will gen a rand num (0 or 1) and if it is 0 assign X, or if it is 1, assign O.
There should be 4 X's or 5 x's.
The ASCII value of X is 88 and the ASCII value of O is 79.
Instead of testing for whether or not they all equal 4 or 5, just test
if ((topL + topC + topR + midL + midC + midR + botL + botC + botR)
> 756 )
and
if ((topL + topC + topR + midL + midC + midR + botL + botC + botR)
< 747 )
This saves space because you don't have to reassign the value and THEN test. You just assign once and test based on the ASCII values. Which would also means all of your Ints need to be Char variables.
And dear lord all those Else If's. Just make a couple if else if's and do X or O is winner for each.
Last edited by Ellondu; 03-18-2012 at 01:07 AM.
> How? In tic-tac-toe, each cell can have three states: empty, circle and cross.
Now why do you suppose I named one of the variables xboard?
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.