Tic tac toe game

closed account (4yAoLyTq)
Hi, I am a beginner in c ++, I'm now confused make tic tac toe game without using arrays and loops, but using pass by value and pass by reference.
Hello,
What have you got by now?
I'm now confused make tic tac toe game without using arrays and loops, but using pass by value and pass by reference.


did you study functions? if not you should + learn passing values by reference.

without using arrays and loops


you cant avoid using arrays and loops in a tic tac toe game how will you store the information? how will you counts turns? etc..

maybe consider making the game with keypads for example:

1|2|3
-------
4|5|6
-------
7|8|9

lets say its x's turn he can use any of the following keys 1-9 to put an x but only if the spot isn't occupied and than its o's turn and it will loop until there is a row of 3 (x's or o's) or its a tie and no one wins.

at this point don't bother with cursors that allow arrow key manipulation in console and don't bother with multi files but it can be a good practice to implement them when your base game works.
than you may consider adding a menu and a bot with difficulties (min max algorithm is most common for player vs pc)
Why would you want to make tic-tac-toe without arrays or loops? I guess this is because this is a requirement for a homework assignment, but in that case it would be appreciated if you also mentioned this and the subject of the recent lectures. As that is usually a good indicator for the desired solution.

For tic-tac-toe you will need to contain some data (the state of the 9 positions). So if you don't want to use an array, you will have to find another container. A struct, class-object or list for example.

If you don't want to use loops, have a look at using recursion.
I thinks it's possible to do it without loops and arrays but very tedious.

The game has 9 cells, so if you don't want to use an array you need 9 individual variables - like cel11, cell12 etc.. where first digit is the row and the second digit the column.

cell11then means cell at row 1 and col 1

To check the first row for example you would need sth, like this:
1
2
3
4
5
if (cell11 == 'X'  && cell12 == 'X' && cell13 == 'X')
  // X has won


    
Ok so how are you going to track player turns ?
how will you check for this condition? only a loop or a complex recursive function can check each turn if the condition
if (cell11 == 'X' && cell12 == 'X' && cell13 == 'X')
is fulfilled.
and lastly if your a beginner why do you seek for the hardest if not impossible way instead of learning the correct way ?

NOTE:if you would do the program without any loops your problems will be as following:
1)the programs length will probably be thousands of lines in length because you will need to check for 9 conditions each turn as there is 9 options to lay out x's or o's to win.
2)there is a minimum of 3 turns to win and a maximum of 9 turns to win and in the case of 9 it needs another check as it can be a draw aswell.
3)you need to track somehow which players turn it is (x's or o's) just to keep in mind as you don't want x or o to have infinite turns right? (more correctly you wont want a player to be allowed to put 3 x's in a line without o having a single turn but if you don't want this just cross it out) from here we can come with a conclusion that you need to implement some sort of algorithm to not allow x to be put in a cell when its o's turn and vise versa.
4)if you would want to implement a player vs pc option without loops or arrays a minmax algorithm would become huge in size and very hard if not impossible to make (keep in mind that if you want a menu its a few extra lines of code).
5)lastly using this method of approach is very error prone

if you want everything mentioned above in your code without loops or arrays i would estimate your program about 2000 lines of code (*important: can be shortened substantially with functions)
if your a beginner why do you seek for the hardest if not impossible way

Probably it is in the homework assignment. In which case I think the assignment is a poor one, if not pure evil.

1. I think you have to check for only 5 conditions: |, -, /, \ and full.

3. Very important, the player should be determined by a parameter in the (recursive) function call, or you would have to count the number of x-es and o's on the board each time to determine whose turn it is

5. Very true

Completely agree that programming a game like tic-tac-toe without using functions would be ridiculous.

Hi, could you give me some guidance regarding the same project?
I'm a little bit farther along tho, http://www.cplusplus.com/forum/beginner/199553/
@globaltourist I gave it a try and ended up with 264 lines, including white lines between functions and classes and placing every curly bracket on its own line. It could be reduced to about 100 lines if I remove the classes and interfaces I used for the AI and administration of scores and names (not required to allow two people to play the game).

I think if you try you can do it with about 70 lines of code without making a mess.
Last edited on
as i wrote above
*important: can be shortened substantially with functions
now try doing that without functions and classes :)
- and just interested to know did you try implementing a min max algorithm for the Ai or its not required ?

NOTE: just to add to your previous comment try implementing everything i wrote in my previous replay (with strict turns, smart ai and no use of functions and loops) and i can guarantee it will become ridiculous very fast.
my point is that maybe i just misunderstood what you were trying to achieve as what you described in your last comment is not what i thought you were trying to achieve.
Last edited on
Topic archived. No new replies allowed.