Beispiel #1
0
    def test_O_win(self):
        game = TicTacToe()
        game.mark(0, 1), game.mark(1, 2)
        game.mark(0, 2), game.mark(1, 1)
        game.mark(2, 0), game.mark(1, 0)

        winner = game.winner()
        self.assertEqual(winner, 'O')
Beispiel #2
0
def main():
    moves1 = ["1", "2", "5", "9", "7", "4", "8", "3", "6"]
    moves2 = ["1", "2", "3", "4", "5", "6", "8", "7", "9"]
    moves3 = ["2", "1", "3", "5", "4", "9", "6", "7", "8"]

    print("------------------------------------------")
    print("TicTacToe using array implementation.     ")
    print("------------------------------------------")
    ttt = TicTacToe(True)
    ttt.Run("o", moves1)
    ttt.Run("o", moves2)
    ttt.Run("o", moves3)
    # Interactive run. Will fail in Jenkins.
    # ttt.Run()

    print("------------------------------------------")
    print("TicTacToe using dictionary implementation.")
    print("------------------------------------------")
    tttDict = TicTacToeUsingDict(True)
    tttDict.Run("x", moves3)
    tttDict.Run("x", moves2)
    tttDict.Run("x", moves1)
Beispiel #3
0
def application(environ, start_response):
    headers = [('Content-Type', 'text/html; charset=utf-8')]
    path = environ['PATH_INFO']
    params = urllib.parse.parse_qs(environ['QUERY_STRING'])
    un = params['username'][0] if 'username' in params else None
    pw = params['password'][0] if 'password' in params else None

    if path == '/register' and un and pw:
        user = cursor.execute('SELECT * FROM users WHERE username = ?',
                              [un]).fetchall()
        if user:
            start_response('200 OK', headers)
            page = '''<!DOCTYPE html>
                                      <html>
                                      <head></head>
                                      <body>
                                          Sorry, username "{}" is taken, Try Again
                                          <br>
                                          
                                          <a href="/">Home</a>
                                      </body></html>'''.format(un)
            return [page.encode()]
        else:
            connection.execute('INSERT INTO users VALUES (?,?)', (un, pw))
            connection.commit()
            start_response('200 OK', headers)
            headers.append(('Set-Cookie', 'session={}:{}'.format(un, pw)))
            page = '''<!DOCTYPE html>
                                      <html>
                                      <head></head>
                                      <body>
                                          Congratulations, username {} been successfully registered
                                          <br>
                                          <a href="/play">Let's go to Tic-Tac-Toe</a>
                                      </body></html>'''.format(un)
            return [page.encode()]

    elif path == '/login' and un and pw:
        user = cursor.execute(
            'SELECT * FROM users WHERE username = ? AND password = ?',
            [un, pw]).fetchall()
        if user:
            headers.append(('Set-Cookie', 'session={}:{}'.format(un, pw)))
            start_response('200 OK', headers)
            page = '''<!DOCTYPE html>
                            <html>
                            <head></head>
                            <body>
                                User {} successfully logged in.
                                <br>
                                <a href="/play">Let's go to Tic-Tac-Toe</a>
                            </body></html>'''.format(un)
            return [page.encode()]
        else:
            start_response('200 OK', headers)
            return [
                login_page.format(
                    "You have input wrong username or password, Try Again.").
                encode()
            ]

    elif path == '/logout':
        headers.append(
            ('Set-Cookie', 'session=0; expires=Thu, 01 Jan 1970 00:00:00 GMT'))
        start_response('200 OK', headers)
        return [login_page.format("Wanna Play Again? Login First!").encode()]

    elif path == '/play':
        start_response('200 OK', headers)

        if 'HTTP_COOKIE' not in environ:
            return [login_page.format("Wanna Play? Login First!").encode()]

        cookies = http.cookies.SimpleCookie()
        cookies.load(environ['HTTP_COOKIE'])
        if 'session' not in cookies:
            return [login_page.format("Wanna Play? Login First!").encode()]

        [un, pw] = cookies['session'].value.split(':')
        user = cursor.execute(
            'SELECT * FROM users WHERE username = ? AND password = ?',
            [un, pw]).fetchall()

        if user:
            page = str(
                '<!DOCTYPE html><html><head><title>Tic-Tac-Toe</title></head><body style="text-align:center;">'
                + '<h2>Tic-Tac-Toe</h2>')

            theBoard = [
                '', '<input type="radio" name="playerMove" value="1">',
                '<input type="radio" name="playerMove" value="2">',
                '<input type="radio" name="playerMove" value="3">',
                '<input type="radio" name="playerMove" value="4">',
                '<input type="radio" name="playerMove" value="5">',
                '<input type="radio" name="playerMove" value="6">',
                '<input type="radio" name="playerMove" value="7">',
                '<input type="radio" name="playerMove" value="8">',
                '<input type="radio" name="playerMove" value="9">'
            ]

            if game.turn == 'new':
                game.turn = game.whoGoesFirst()

            if game.turn == 'X':
                if 'playerMove' not in params:
                    page += '<br>It is X\'s turn<br>'
                    page += TicTacToe.drawBoard(game, game.board)
                    return [page.encode()]
                else:
                    page += '<br>It is O\'s turn<br>'
                    playerMove = params['playerMove'][0]
                    game.board = game.makeMove(game.board, 'X', playerMove)
                    page += game.drawBoard(game.board)
                    game.turn = 'O'
                    if game.isWinner(game.board, 'X'):
                        page = '''<!DOCTYPE html><html><head><title>TTT Game</title></head>
                                  <body style="text-align:center;">                        
                                  <h2>Tic-Tac-Toe</h2><br>                                 
                                  Player X wins!<br>                                   
                                  <a href="/play">Play again</a><br>                       
                                  <a href="/logout">Log out</a>'''
                        game.board = theBoard
                        game.turn = 'new game'
                        return [page.encode()]
                    elif game.isFull(game.board):
                        page = '''<!DOCTYPE html><html><head><title>TTT Game</title></head>
                                  <body style="text-align:center;">                        
                                  <h2>Tic-Tac-Toe</h2><br>                                 
                                  The game is a tie.<br>                                   
                                  <a href="/play">Play again</a><br>                       
                                  <a href="/logout">Log out</a>'''
                        game.board = theBoard
                        game.turn = 'new'
                        return [page.encode()]
                    else:
                        return [page.encode()]

            elif game.turn == 'O':
                if 'playerMove' not in params:
                    page += '<br>It is O\'s turn<br>'
                    page += TicTacToe.drawBoard(game, game.board)
                    return [page.encode()]
                else:
                    page += '<br>It is X\'s turn<br>'
                    playerMove = params['playerMove'][0]
                    game.board = game.makeMove(game.board, 'O', playerMove)
                    page += game.drawBoard(game.board)
                    game.turn = 'X'
                    if game.isWinner(game.board, 'O'):
                        page = '''<!DOCTYPE html><html><head><title>TTT Game</title></head>
                                  <body style="text-align:center;">                        
                                  <h2>Tic-Tac-Toe</h2><br>                                 
                                  Player O wins!<br>                                   
                                  <a href="/play">Play again</a><br>                       
                                  <a href="/logout">Log out</a>'''
                        game.board = theBoard
                        game.turn = 'new game'
                        return [page.encode()]
                    elif game.isFull(game.board):
                        page = '''<!DOCTYPE html><html><head><title>TTT Game</title></head>
                                  <body style="text-align:center;">                        
                                  <h2>Tic-Tac-Toe</h2><br>                                 
                                  The game is a tie.<br>                                   
                                  <a href="/play">Play again</a><br>                       
                                  <a href="/logout">Log out</a>'''
                        game.board = theBoard
                        game.turn = 'new'
                        return [page.encode()]
                    else:
                        return [page.encode()]
        else:
            return [
                login_page.format("Wanna Play Game? Login First!").encode()
            ]

    elif path == '/':
        start_response('200 OK', headers)
        page = '''<!DOCTYPE html>
                <html>
                <head></head>
                <body>
                <form action = "/login">
                    Username <input type="text" name="username"><br>
                    Password <input type="password" name="password"><br>
                    <input type="submit" value = 'login'>
                </form>
                <form action = "/register">
                    Username <input type="text" name="username"><br>
                    Password <input type="password" name="password"><br>
                    <input type="submit" value = 'register'>
                </form>
                </body></html>'''
        return [page.encode()]

    else:
        start_response('404 Not Found', headers)
        return ['Status 404: Resource not found'.encode()]
Beispiel #4
0
except:
    pass
cursor = connection.cursor()

theBoard = [
    '', '<input type="radio" name="playerMove"  value="1">',
    '<input type="radio" name="playerMove"  value="2">',
    '<input type="radio" name="playerMove"  value="3">',
    '<input type="radio" name="playerMove"  value="4">',
    '<input type="radio" name="playerMove"  value="5">',
    '<input type="radio" name="playerMove"  value="6">',
    '<input type="radio" name="playerMove"  value="7">',
    '<input type="radio" name="playerMove"  value="8">',
    '<input type="radio" name="playerMove"  value="9">'
]
game = TicTacToe(theBoard)
game.turn = 'new'
turnMap = {0: "X", 1: "O"}
login_page = '''<!DOCTYPE html>
                       <html>
                       <head></head>
                       <body>
                       <h1>{}</h1>
                       <form action = "/login">
                           Username <input type="text" name="username"><br>
                           Password <input type="password" name="password"><br>
                           <input type="submit" value = 'login'>
                       </form>
                       <br>
                       <a href="/">Home Page</a>
                       </body></html>'''
Beispiel #5
0
    def test_draw(self):
        game = TicTacToe()

        game.mark(1, 1), game.mark(0, 2)
        game.mark(2, 2), game.mark(0, 0)
        game.mark(0, 1), game.mark(2, 1)
        game.mark(1, 2), game.mark(1, 0)
        game.mark(2, 0)

        winner = game.winner()
        self.assertEqual(winner, None)
Beispiel #6
0
from randomComputerPlayer import RandomComputerPlayer
from ticTacToe import TicTacToe
from humanPlayer import HumanPlayer
from gameController import GameController

if __name__ == "__main__":
    x_player = RandomComputerPlayer("X")
    o_player = HumanPlayer("O")
    tic = TicTacToe()
    GameController.play(tic, x_player, o_player, print_game=True)
Beispiel #7
0
from ticTacToe import TicTacToe
from playerRand import PlayerRand
from playerReact import PlayerReact
from playerUnbeat import PlayerUnbeat
import matplotlib.pyplot as plt
import math

playerVariants = [PlayerRand, PlayerReact, PlayerUnbeat]
playerLabels = ["Random", "Reactive", "Unbeatable"]
game = TicTacToe()
numIterations = 1000

plt.close('all')
_, plots = plt.subplots(3, 3)


def getLabels(a, valA, b, valB):
    return [("P1 " + playerLabels[a] + " wins") if valA > 0 else "", "Draw",
            ("P2 " + playerLabels[b] + " wins") if valB > 0 else ""]


def pieFilter(pct):
    return ('%1.1f%%' % pct) if pct > 0 else ""


def printProgress(string, part, whole, indent=50, size=20):
    indent -= int(math.log(part / whole * 100, 10))
    out = "\r" + string + " " * (indent - len(string))
    out += "%.0f%%" % (part / whole * 100)
    hashNum = int(size * part / whole)
    out += " [" + "#" * hashNum + " " * (size - hashNum) + "]"