1 Replies - 977 Views - Last Post: 14 February 2013 - 04:15 PM Rate Topic: -----

#1 Plaflamme128  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 13-February 13

4x4 Tic Tac Toe off of what I already have?

Posted 13 February 2013 - 06:31 PM

I have been working at this program for a while and I cant seem to figure it out. I'm not sure what i'm doing wrong. I have to make this 3x3 grid into a workable 4x4 grid for tic tac toe.

I've tried changing the values, and lengths, but I cant seem to get it right. Any help would be appreciated.


""" This program will play a game of Tic-Tac-Toe. It will play at least one
game and then let the user decide whether to play additional games.

Version 1.0 has just the top-level logic.

Version 2.0 introduces the TTTgame class and shows some basic techniques
for defining objects and using them.

Version 3.0 starts adding attributes to the game and fleshes out some basic
functions.

Version 4.0 concentrates on the getMove method and the draw method. We will
figure out winning in the next version.

Version 5.0 figures out if someone has won the game by getting 3 in a line.

"""

def clearScreen():
    for i in range(80):
        print
    

def displayBoard(L):
    # clearScreen()
    print ' '+str(L[0]) + ' | ' + str(L[1]) + ' | ' + str(L[2])
    print '---+---+---'
    print ' '+str(L[3]) + ' | ' + str(L[4]) + ' | ' + str(L[5])
    print '---+---+---'
    print ' '+str(L[6]) + ' | ' + str(L[7]) + ' | ' + str(L[8])
    print 

def contains(L1,L2):
    for i in L2:
        if i not in L1:
            return False
    return True
    

PlayAgain = True

class TTTgame:
    def __init__(self):
        self.board = range(1,10)
        self.player = 'X'
        self.free = range(1,10)
        self.moves = {'X' : [ ], 'O' : [ ] }

    def won(self,p):
        winners = [ [1, 2, 3], [4, 5, 6], [7, 8, 9], [1, 4, 7],
                    [2, 5, 8], [3, 6, 9], [1, 5, 9], [3, 5, 7] ]
        mvs = self.moves[p]
        for w in winners:
            if contains(mvs,w):
                return True
        return False


    def draw(self):
        return len(self.free) == 0

    def getMove(self,p):
        legalMove = False
        while not legalMove:
            mv = raw_input("Please enter a legal move for "+p+'. ')
            if (len(mv) == 1) and (mv in '123456789') and int(mv) in self.free:
                legalMove = True
        mv = int(mv)
        self.moves[p].append(mv)
        self.free.remove(mv)
        self.board[mv-1] = p

    def play(self):
        gameOver = False
        while not gameOver:
            displayBoard(self.board)
            if self.won('X'):
                print "X won!"
                gameOver = True
            elif self.won('O'):
                print "O won!"
                gameOver = True
            elif self.draw():
                print "The game is a draw!"
                gameOver = True
            elif self.player == 'X':
                self.getMove('X')
                self.player = 'O'
            else:
                self.getMove('O')
                self.player = 'X'

while PlayAgain:
    TTT = TTTgame()
    TTT.play()
    YN = raw_input("Do you want to play again (Y/n)? ").lower()
    if 'y' not in YN:
        PlayAgain = False

print "Game Over"



This post has been edited by Simown: 14 February 2013 - 11:05 AM
Reason for edit:: Fixed code tags


Is This A Good Question/Topic? 0
  • +

Replies To: 4x4 Tic Tac Toe off of what I already have?

#2 woooee  Icon User is offline

  • D.I.C Head

Reputation: 27
  • View blog
  • Posts: 109
  • Joined: 21-November 12

Re: 4x4 Tic Tac Toe off of what I already have?

Posted 14 February 2013 - 04:15 PM

You posted the 3X3 program. Post the 4X4 program and state specifically what and where the problems are.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1