•

Check out our Community Blogs

Register and join over 40,000 other developers!

### Recent Blog Entries

• phi

I love this community !

• JackJames

hi i am jack i am seo expert jack james would love you to read new post

# "Tic-Tac-Toe-Cover-Up" game coding dilemma!

python tic-tac-toe game game design game programming

This topic has been archived. This means that you cannot reply to this topic.
6 replies to this topic

### #1 contron

contron

CC Lurker

• New Member
• 4 posts

Posted 17 October 2013 - 08:15 PM

Hello internetz!

This is my first post on here so I apologize if its sloppy!  So basically, heres my code.  The game is a version of tic-tac-toe that uses a slightly different game mechanic.  The game is called tic-tac-toe-cover-up and the only difference it between the original game is that if you choose a space that is already occupied by your opponents piece you "cover-up" their piece with the next biggest piece in your piece set. Here are the "official" rules to my game.

RULES

1.  The X pieces are ['*','x','X'] while the O pieces are ['.','o','O']

2.  You can only play the smallest piece ('*' or '.') in an empty space.

3.  If the largest piece is in a tile, you cannot cover the piece.

4.  Playing a piece already on a space will "cover-up" the piece with the next largest piece from YOUR set. ('x' covers up '.')

5.  The winning is the same as tic-tac-toe (three in a row... lol, duh), however, your pieces can all be different sizes.

My problem is that I'm having trouble checking for a winner because the check function compares each space with the next sequential space.  So when it runs into a case where say both '*' and 'x' are present.  The computer doesn't recognize that both '*' and 'x' are a subset of the moves of player X.

I know I could go through and hard-code every single case by using my for loop already in place, but I feel like there may be a faster way... no?  I'm really not trying to change a lot of my code I already have, just the check_done(  ) function.  Mad props to anyone who can find a solution before I just cave and hard-code it.

NOTE: I realize I don't have error checking, it was causing errors with where I originally placed it in the code because of the except: loop.

```#Checking for a winner
def check_done():
for i in range(0,3):
if map[i][0] == map[i][1] == map[i][2] != " " \
or map[0][i] == map[1][i] == map[2][i] != " ":
print turn, "won!!!"
return True

if map[0][0] == map[1][1] == map[2][2] != " " \
or map[0][2] == map[1][1] == map[2][0] != " ":
print turn, "won!!!"
return True

if " " not in map[0] and " " not in map[1] and " " not in map[2]:
print "Draw"
return True

return False

#Prints the current board
def print_board():
for i in range(0,3):
for j in range(0,3):
print map[2-i][j],
if j != 2:
print "|",
print ""

#Map and constant value assignments
map = [[" "," "," "],
[" "," "," "],
[" "," "," "]]

done = False

#List X and O moves
xMoves = ["*","x","X"]
oMoves = [".","o","O"]

#Opening greeting message
print "Welcome to Tic-Tac-Toe-Cover-Up!"
player1Symbols = raw_input("Player 1, please select either X or O for your pieces: ")
print

#Assigning player moves
if (player1Symbols == "X"):
player1Symbols = xMoves
player2Symbols = oMoves
turn = "X"
else:
player1Symbols = oMoves
player2Symbols = xMoves
turn = "O"

#Main looping function for the game.
while not done:
print_board()

print turn,"'s turn"
if (turn == "X"):
playerMoves = xMoves
opponentMoves = oMoves
else:
playerMoves = oMoves
opponentMoves = xMoves
print

print "Please select position by typing in a number between 1 and 9, see below for which number that is for which position..."
print "7|8|9"
print "4|5|6"
print "1|2|3"
print

pos = input("Select: ")
if pos <=9 and pos >=1:
Y = pos/3
X = pos%3
if X != 0:
X -=1
else:
X = 2
Y -=1

if map[Y][X] == " ":
map[Y][X] = playerMoves[0]
moved = True
done = check_done()
elif map[Y][X] != " ":
if map[Y][X] == opponentMoves[0]:
map[Y][X] = playerMoves[1]
moved = True
done = check_done()
if map[Y][X] == opponentMoves[1]:
map[Y][X] = playerMoves[2]
moved = True
done = check_done()

if done == False:
if turn == "X":
turn = "O"
else:
turn = "X"

```

Heres the code if anyone wants to play around with it.  I feel like with a GUI this code be a fun little game.

Edited by contron, 17 October 2013 - 08:30 PM.

### #2 BlackRabbit

BlackRabbit

CodeCall Legend

• Expert Member
• 3871 posts

Posted 17 October 2013 - 11:39 PM

Welcome aboard Contron!

Nice game! There's one thing I don't get from the rules, is the winner defined after the last piece is played? can you win before?

### #3 WingedPanther73

WingedPanther73

A spammer's worst nightmare

• Moderator
• 17757 posts

Posted 18 October 2013 - 08:20 AM

I would seperate the display from the internal game state representation. If you use an int to represent game state, you can use 0 to represent blank, 1,3,5 to represent the x's, and 2,4,6 to represent the o's. Then your comparison can be against 0, space%2 == 1, and space%2==0.

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/

### #4 contron

contron

CC Lurker

• New Member
• 4 posts

Posted 18 October 2013 - 01:40 PM

Welcome aboard Contron!

Nice game! There's one thing I don't get from the rules, is the winner defined after the last piece is played? can you win before?

You can win at any time, so long as you have three in a row of any combination of your pieces.

### #5 BlackRabbit

BlackRabbit

CodeCall Legend

• Expert Member
• 3871 posts

Posted 18 October 2013 - 06:19 PM

I agree with WingedPanter, you need to separate the board view, hence the game state, from the game's history.

But, if you happen to keep it goint as it is, you could change the chips for some "coded" chips, like, player A plays with character which are mod 0 when divided by 2 (even numbers) and player B plays with characters which are mod 1 when divided by 2 (odd numbers)

That could help

### #6 contron

contron

CC Lurker

• New Member
• 4 posts

Posted 20 October 2013 - 12:35 PM

I would seperate the display from the internal game state representation. If you use an int to represent game state, you can use 0 to represent blank, 1,3,5 to represent the x's, and 2,4,6 to represent the o's. Then your comparison can be against 0, space%2 == 1, and space%2==0.

I get what you are saying, however, what would I need to initialize my map to so that it isn't comparing against types? For instance, 0,0, and " ".

### #7 contron

contron

CC Lurker

• New Member
• 4 posts

Posted 20 October 2013 - 01:00 PM

Solved the problem! Thanks for all the advice but what actually ended up being the easiest was having a second map that only recorded an "X" or an "O" depending on whether the move came from the o-set of moves or the x-set of moves, thus it compared a "X" to an "*" in display, but "X" to "X" in the check function.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download