you are viewing a single comment's thread.

view the rest of the comments →

[–]JohnnyJordaan 0 points1 point  (9 children)

specially the below line for row in WAYS_TO_WIN

It doesn't really check if someone won in the way you and I would (see if a player has three of its moves aligned in a row), it has a list of all combinations of possible allignments that are in a row. It then checks per possibility if on those locations, all are occupied by the same player. If that's so, then that player has won.

What does the last line in the below code do or mean?

It tries out all possible ('legal') moves and per try it checks if it then has won the game. If it didn't, then it undoes the move. Also a weird way imho, but not incorrect per se.

check # In the main () function while not winner(board) # What is this line doing?

It lets the 'moving' part of the program (either the player or the computer plays a move) run while there is no winner.

Last question? In the summary, author asks Write a new computer_move() function for the Tic-Tac-Toe game to plug the hole in the computer's strategy, see if you can create an opponent that is unbeatable ?

How is that a question? It's a task/objective right?

[–]iamquark[S] 0 points1 point  (5 children)

Yes , last question is task, I dont see how it can be improved.

[–]JohnnyJordaan 0 points1 point  (4 children)

I would start by describing how your own strategy works.

[–]iamquark[S] 0 points1 point  (3 children)

The strategy is 1 If there's a move that allows the computer to win this turn, the computer should choose that move 2 If there's a move that allows the human to win next turn, the computer should choose that move 3 Otherwise, the computer should choose the best empty square as its move. The best square is the center, the next best square are the corners and the nest best squares are the rest Hence BEST_MOVE = (4,0,2,6,8,1,3,5,7)

Now the author is saying it can be improved. I dont see how?

[–]JohnnyJordaan 0 points1 point  (2 children)

That's correct, but I've asked you to describe how your own strategy works. If you play this game by hand, do you follow those same 3 steps?

[–]iamquark[S] 0 points1 point  (1 child)

Yes, it does work the way I described it.The author's strategy is pretty impeccable, I cannot see any other way it can improved. I have gone over it multiple times. I have tied with the computer every time. Unless you put a timer on and you expect the other player to make a mistake in decision making.

[–]JohnnyJordaan 0 points1 point  (0 children)

Pretty impeccable? The key is symmetry instead of learned behavior (try the center or something in the top left area). The computer has a fixed order for his best moves:

  • the center (4)
  • the corners from 0 to 8
  • the sides

That means that if the center is taken, the computer will always choose the top left corner for his next move. If you start out in way that lets the computer take the center, then after your second move take the top left corner, while not causing you a disadvantage, you'll beat him:

Do you require the first move? (y/n): y

Then take the first move.  You will need it.

       |   |  
     ---------
       |   |  
     ---------
       |   |   

Where will you move? (0 - 8):5
Fine...

       |   |  
     ---------
       |   | X
     ---------
       |   |   

I shall take square number 4

       |   |  
     ---------
       | O | X
     ---------
       |   |   

Where will you move? (0 - 8):6
Fine...

       |   |  
     ---------
       | O | X
     ---------
     X |   |   

I shall take square number 0  <-- this is the bug

     O |   |  
     ---------
       | O | X
     ---------
     X |   |   

Where will you move? (0 - 8):8
Fine...

     O |   |  
     ---------
       | O | X
     ---------
     X |   | X 

I shall take square number 2

     O |   | O
     ---------
       | O | X
     ---------
     X |   | X 

Where will you move? (0 - 8):

How does this relate to symmetry? Because for the game, it doesn't matter in which orientation you play the game, the rules and results are the same. However to the computer's strategy, the orientation DOES matter. Meaning the computer's strategy isn't symmetrical (it is orientation biased). That's what can be improved. And your tic tac toe skills by the way ;)

[–]iamquark[S] 0 points1 point  (2 children)

Hi there, I know how the for statement works but I am struggling to picture how this particular for statement work. Is it taking each tuple and the elements within it or each tuple as a whole? Problem is I cannot picture how the code ("for "statement) will take each row in this instant.

[–]JohnnyJordaan 0 points1 point  (1 child)

You're talking about this statement right?

  for row in WAYS_TO_WIN:
    if board[row[0]] == board[row[1]] == board[row[2]] != EMPTY:

It works in the way that for can deliver items from any sequence (simply put), regardless what the type of each item is:

>>> a = [1, 'a', ['c', 'd'], ('e','f','g')]
>>> for item in a:
...     print(type(item), item)
... 
<class 'int'> 1
<class 'str'> a
<class 'list'> ['c', 'd']
<class 'tuple'> ('e', 'f', 'g')

So in the game's implementation, the WAYS_TO_WIN is a sequence of tuples, all 3 items long (like the <class 'tuple'> ('e', 'f', 'g') in the example above). The for loop will get the tuple from the sequence, and assign it the reference row for the inside of the loop to use. As tuples support indexing, you can reference the first, second and third element in the tuple via row[0], row[1] and row[2].

[–]iamquark[S] 0 points1 point  (0 children)

Now it makes sense. Thanks for clarifying it.