MISTERCEC
MISTERCEC

Reputation: 53

simple help in tic-tac-toe python

I'm creating a program that manages a Tic-tac-toe game, I'm creating a list of lists

[['', '', ''], 
 ['', '', ''], 
 ['', '', '']] 

to create the game grid, I would like the program to stop when it finds a match like

[['x', 'x', 'x'],
 ['', '', ''],
 [ '', '', '']

In this case for example, I would therefore need to be able to say that in the other boxes there could be any character, 'o', 'x', ''

I would that python if the grid is a winning grid the program must stop

Upvotes: 1

Views: 1089

Answers (2)

6502
6502

Reputation: 114481

What you would do to check "by hand" ?

  1. check first row if all three positions are the code of the player

    if b[0][0]==code and b[0][1]==code and b[0][2]==code: return True

  2. check second row

  3. check third row

  4. check first column if all three are the code of the player

    if b[0][0]==code and b[1][0]==code and b[2][0]==code: return True

  5. check second and third column too

  6. check first and second diagonal too

for the rows and cols is easy to write a loop instead of copy-paste the code

There are also other ways to write either shorter code (using things like all(b[i][j] == code for j in range(3))) or faster code (using things like bits instead of characters and a single integer for all xs or os).

Another "trick" would be keeping the board in a single array instead of a matrix and using a placeholder (e.g. '-') for empty squares

board = ['-', '-', '-',
         '-', '-', '-',
         '-', '-', '-']

then you can check for winning strikes using a regular expression

s = "".join(board) # change to a single string
if re.match("xxx......", s) return True # first row check
if re.match("...xxx...", s) return True # second row check
if re.match("......xxx", s) return True # third row check
if re.match("x..x..x..", s) return True # first col check
if re.match(".x..x..x.", s) return True # second col check
if re.match("..x..x..x", s) return True # third col check

What I would do is using bits: the board is represented by two numbers 0...511: one number for where the x marks are and another for where the o marks are:

# board bits are
#
#     1      2      4
#     8     16     32
#    64    128    256
#
Xs = Os = 0 # empty board

win_codes = [1+2+4, 8+16+32, 64+128+256, # rows
             1+8+64, 2+16+128, 4+32+256, # cols
             1+16+256, 4+16+64]          # diags

def wins(pos):
    return any((pos & c) == c for c in win_codes)

given that the total number of positions is also small (512) the winning ones can also be precomputed and stored in a table

win_pos = [wins(pos) for pos in range(512)]

then the check is simply

if win_pos[Xs]: ...  # player X won the game

Upvotes: 1

Restioson
Restioson

Reputation: 131

If I have correctly interpreted what you are trying to say, you don't need to. Python is not statically-typed -- that is, any variable can hold any type. In a statically typed language, you would need to declare (or have it inferred for you) that your game grid is of type list of list of char, or equivalent. So, you can just set the element at any position to be whatever you want -- including characters like 'o' and 'x'.

TL;DR you don't need to.

Note: it is generally considered bad style to change the type of a variable in the middle of a program for no reason, as this can confuse you and other people.

Upvotes: 0

Related Questions