Ask coding questions

← Back to all posts
CPU not playing sometimes (tic tac toe)
h
CodingCactus (4087)

When playing against cpu, sometimes they don't make a move. I've checked every thing, maybe a fresh pair of eyes will help, or a completely different approach altogether.

If you want to see the error, 2,1, 2,2, anything; normally causes it, but other things do as well I just noticed this one specifically.

Please help if you can!!

new version using 2d array (still have same issue)
https://repl.it/@CodingCactus/Tic-Tac-Toe-1

Answered by Coder100 (15373) [earned 5 cycles]
View Answer
Comments
hotnewtop
Coder100 (15373)

hmmm...
That's some really verbose code you have there...
Try to put it inside a 2d array, it'll make things easier:

[
  [a1, a2, ...],
  [b1, b2, ...],
  [c1, c2, ...]
]

And then a1,b1,... are all strings
like

[
  ["x", " ", "o"],
  ["o", "x", "x"],
  ["o", "x", "o"]
]

and it will make the ai and checking process a lot easier...

Anyways, I think the problem is that there should be more if statements covering all the possibilities (line 211)

CodingCactus (4087)

@Coder100 if i'm honest with you, i don't know what a 2d array is. sorry. And im pretty sure i have enough if statements because of the while loop at line 407

Coder100 (15373)

A 2d array has nesting of 1
For example,

[1,2,3,4]

is a 1d array, because if each entry represented a point, all the points would be in the same spot, there is no x or y value
in a 2d array,

[
[1,2],
[3,4],
[5,6]
]

and each entry was a point, we have points
(1, 2), (3, 4), and (5, 6)
which can be put in a 2 dimensional graph
@CodingCactus

CodingCactus (4087)

OK, how would I implement this my my code?
@Coder100

CodingCactus (4087)

I'm sorry, this is probably frustating for you, but i don't under stand, do you mean like this:?
https://repl.it/@CodingCactus/2D-array-tic-tac-toe
@Coder100

Coder100 (15373)

Good start

gameboard = [
  [" ", " ", " "],
  [" ", " ", " "]
  [" ", " ", " "]
 ]

The board should be empty.
Because, at the start, the board is empty.
@CodingCactus

CodingCactus (4087)

I want the board to be spaced out, (just for cosmetics) which is why it has the spaces in the board at start and the numbers on the side are for the player to know to coordinates for the squares
@Coder100

CodingCactus (4087)

I have this which I think is how I should implement it?
https://repl.it/@CodingCactus/Tic-Tac-Toe-1
@Coder100

Coder100 (15373)

ah ok now replace the 1 2 3 with x or os and remember: the game board has to be empty at the start
@CodingCactus

CodingCactus (4087)

what will replacing the 1 2 3 with x or os do?
@Coder100

Coder100 (15373)

visual display and also, what does 1 2 3 mean?
This is a tic-tac-toe game so xs and os @CodingCactus

CodingCactus (4087)

You see it when you run it, it's the numbers down the side and top of the board so that the player knows the coordinates of the place they want to put their letter, I realise what you said about the X's and os , I was being a bit stupid there (by accident)
@Coder100

Coder100 (15373)

ohhhhh
i dont think that is necessary
You can get a coordinate like

game[x][y]

@CodingCactus

CodingCactus (4087)

It is just for the user so that they know what to input.
@Coder100

Coder100 (15373)

still confused but it's your code so whatever you want to do I guess @CodingCactus

CodingCactus (4087)

ok, it doesn't really matter. But I am still having trouble with the original problem of the ai not playing on some turns
@Coder100

Coder100 (15373)

yeah, i know
so now, you can do it in an easier format

if(move==...)

and so on
and you can loop through all of the squares and eventually map out all possibilities
@CodingCactus

CodingCactus (4087)

ok, thanks for help, i'll do that and see what happens
@Coder100

[deleted]

@CodingCactus Is is fine now?

CodingCactus (4087)

@MrHackman no, same problem

edit: works now

Thecrowbar1234 (140)

use arrays instead of a string for the board

JosiahKnisely (32)

Worked fine for me. Your code is very ahhh... Finicky? Try using a 2d array.

CodingCactus (4087)

@JosiahKnisely I'm going to try that today thanks.

CodingCactus (4087)

@Coder100 It works now, yay. the problem was not that it was missing a turn, but writing over the previous "o", I've fixed that now by checking if there is anything in the box that is about to be written in.

THANK YOU FOR ALL YOUR HELP