Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
moves in Tic-Tac-Toe
#1
I have a string "x-xoxoo--" which meeans a satge in a game Tic-Tac-Toe

x|-|x
 o|x|o
o|-|-
I know how to check if that one element in a list is valid:

from math import *
def check(play):
        x=0
        o=0
        for item in paly:
            if item=="x":
                x=x+1
            if item=="o":
                o=o+1
        return (fabs(x-o)<=1)
I want to check if for example a list
 ["x-xoxoo--", "x-xoxoox-",  "x-xoxooxo"]
describes the possible sequence of consecutive moves in the game?

For example
["----x----", "o---x----", "oo--x----"] is not valid

["----xo---", "o---xo---", "x---xo---"] is not valid
Reply
#2
For it to be valid the number of x's minus the number of o's must be 1 or 0. If it's 1, x made the last move; if it's 0, o made the last move. So you can't have a win from the player who didn't make the last move. If x just moved, only x can have a win. Finally, you can't have two independent wins. That is, if there are two wins, they must share a square, which would have been the last move made. You can have:

Output:
xoo oxo xxx
But you can't have:

Output:
xxx ooo xxx
But you don't really have to worry about that for standard tic-tac-toe, because in the 3x3 case independent wins also have too many x moves.

So you need to count the pieces, and you need to check for wins and make sure they match the final player.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Yes but I have to check only if for example that list ["----x----", "o---x----", "oo--x----"] describes the possible sequence of consecutive moves. Differences between stages are 1. But ["----x----", "o---x----", "oo--x----"] is not valid .
Reply
#4
Is the first position considered valid no matter what? If so, you check the first position to see who's move it is, and then check that only one character has changed, and it changed to the correct character.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Process Moves WalkRhymes 1 1,999 Sep-11-2020, 09:58 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020