コード例 #1
0
def test_cannot_place_over_another_tile():
    ttt = TicTacToe()
    ttt.board = {
        1: {
            1: 'X',
            2: 'O',
            3: ' '
        },
        2: {
            1: ' ',
            2: ' ',
            3: ' '
        },
        3: {
            1: ' ',
            2: ' ',
            3: ' '
        }
    }
    ttt.GAME_STATE = ttt.STATES.CROSS_TURN
    assert ttt.place_marker("X", 1, 1) == False
コード例 #2
0
def test_place_marker():
    ttt = TicTacToe()
    ttt.board = {
        1: {
            1: 'X',
            2: 'O',
            3: ' '
        },
        2: {
            1: ' ',
            2: ' ',
            3: ' '
        },
        3: {
            1: ' ',
            2: ' ',
            3: ' '
        }
    }
    ttt.GAME_STATE = ttt.STATES.CROSS_TURN
    assert ttt.place_marker("X", 1, 3)
    newBoard = {
        1: {
            1: 'X',
            2: 'O',
            3: 'X'
        },
        2: {
            1: ' ',
            2: ' ',
            3: ' '
        },
        3: {
            1: ' ',
            2: ' ',
            3: ' '
        }
    }
    assert ttt.GAME_STATE == ttt.STATES.NAUGHT_TURN
    assert ttt.board == newBoard
コード例 #3
0
def test_valid_input_marker():
    ttt = TicTacToe()
    ttt.board = {
        1: {
            1: 'X',
            2: 'O',
            3: ' '
        },
        2: {
            1: ' ',
            2: ' ',
            3: ' '
        },
        3: {
            1: ' ',
            2: ' ',
            3: ' '
        }
    }
    ttt.GAME_STATE = ttt.STATES.CROSS_TURN
    assert ttt.place_marker("Y", 2, 2) == False
    assert ttt.GAME_STATE == ttt.STATES.CROSS_TURN
コード例 #4
0
def test_out_of_bounds():
    ttt = TicTacToe()
    ttt.board = {
        1: {
            1: 'X',
            2: 'O',
            3: ' '
        },
        2: {
            1: ' ',
            2: ' ',
            3: ' '
        },
        3: {
            1: ' ',
            2: ' ',
            3: ' '
        }
    }
    ttt.GAME_STATE = ttt.STATES.CROSS_TURN
    assert ttt.place_marker("X", 4, 4) == False
    assert ttt.GAME_STATE == ttt.STATES.CROSS_TURN
コード例 #5
0
def test_my_turn():
    ttt = TicTacToe()
    ttt.board = {
        1: {
            1: 'X',
            2: 'O',
            3: ' '
        },
        2: {
            1: ' ',
            2: ' ',
            3: ' '
        },
        3: {
            1: ' ',
            2: ' ',
            3: ' '
        }
    }
    ttt.GAME_STATE = ttt.STATES.CROSS_TURN
    assert ttt.place_marker("O", 1, 1) == False
    assert ttt.GAME_STATE == ttt.STATES.CROSS_TURN
コード例 #6
0
def update_record():
  record = json.loads(request.data)
  partyID = record["party"]
  toreturn = None
  ## game => check it !
  decoded = cipher.decrypt( str(record["encoded"]) )
  print( decoded )
  print( record["board"] )
  if (decoded != str(record["board"])):
    return json.dumps({ "error" : "Invalid Board encryption !", "board": record["board"] })
  
  row = int(record["move2row"])
  col = int(record["move2col"])
  
  print( "row: ", row, "col: ", col )
  
  current_game = TicTacToe()
  current_game.board = record["board"]
  current_game.fix_spot( row-1, col-1, 'X' )
  
  ## AI part / move done by AI !
  tupleByAI = BasicAI.playOn( current_game.board )
  current_game.fix_spot( tupleByAI[0], tupleByAI[1], 'O' )
  
  print( str(current_game.board) )
  encoded = cipher.encrypt( str(current_game.board) )
  print( encoded )
  
  if (current_game.is_player_win( "X" )):
    print( "HUMAN WIN !" )
    toreturn = json.dumps({ "party" : partyID, "board" : current_game.board, "encoded" : encoded, "finished": "Human Win !" })
  elif (current_game.is_player_win( "O" )):
    print( "AI WIN !" )
    toreturn = json.dumps({ "party" : partyID, "board" : current_game.board, "encoded" : encoded, "finished": "AI Win !" })
  else:
    toreturn = json.dumps({ "party" : partyID, "board" : current_game.board, "encoded" : encoded })

  return toreturn
コード例 #7
0
def test_win_diagonal():
    ttt = TicTacToe()
    ttt.board = {
        1: {
            1: 'X',
            2: 'O',
            3: 'X'
        },
        2: {
            1: 'O',
            2: 'X',
            3: 'O'
        },
        3: {
            1: 'O',
            2: 'O',
            3: 'X'
        }
    }
    with pytest.raises(SystemExit) as pytest_wrapped_e:
        ttt.check_for_win()
    assert pytest_wrapped_e.type == SystemExit
    assert pytest_wrapped_e.value.code == 0
コード例 #8
0
def test_win_draw_no_spaces_left():
    ttt = TicTacToe()
    ttt.board = {
        1: {
            1: 'X',
            2: 'O',
            3: 'X'
        },
        2: {
            1: 'O',
            2: 'X',
            3: 'O'
        },
        3: {
            1: 'O',
            2: 'X',
            3: 'O'
        }
    }
    ttt.SPACES_LEFT = 0
    with pytest.raises(SystemExit) as pytest_wrapped_e:
        ttt.check_for_win()
    assert pytest_wrapped_e.type == SystemExit
    assert pytest_wrapped_e.value.code == 1
コード例 #9
0
from socketRecSend import SockSendReceive
from TicTacToe import TicTacToe
import random
import time

plyr = random.randint(0, 1)
ttt = TicTacToe(plyr)
srs = SockSendReceive()

while True:
    # NEED TO GET MULTIPLAYER FIGURED OUT
    # NEED Another file like this for the 2nd player
    "Order goes start server with board. Board gets taken from server. Board gets sent to tic and played for that player. Then send board to next player and repeat"

    # Bug ONLY ONE RUN THROUH WORKS

    # Start Server with board
    srs.rec()

    time.sleep(1)

    # Send board to ttt
    ttt.board = srs.board
    ttt.run()
    srs.board = ttt.board

    time.sleep(1)

    # Send board to next player
    srs.send()