Exemplo n.º 1
0
    def test_countrow(self):
        fake_board = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                      0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0,
                      0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0,
                      0, 0, 0, 0, 2, 1, 1, 0, 0, 0, 0, 0,
                      0, 0, 0, 2, 2, 2, 1, 1, 0, 0, 0, 0,
                      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

        
        g = gomoku.Gomoku(fake_board, 12, 12)
        print(g.count_board(2))
        g.put_p(5,6,1)
        print(g.count_board(1))
        g.put_p(5,6,2)
        print(g.count_board(2))

        self.assertTrue(g.row_startswith(0b101101,0b1011))
        self.assertEqual(g.row_reverse(0b11011),0b11101)
        self.assertEqual(g.row_splice(0b1101001,3,6),0b1001)
        self.assertEqual(g.row_add_p(0b1001,1),0b10011)
        self.assertEqual(g.row_add_p(0b11,0),0b110)

        self.assertEqual(g.count_consec_row(0b1001110),g.patterns[0b101110])
        self.assertEqual(g.count_consec_row(0b10101110),g.patterns[0b101110]+g.patterns[0b101])
Exemplo n.º 2
0
def get_next_move():
    data = request.json
    (x, y) = gomoku.Gomoku(data["board"]).get_next_move(data["cur"])
    response = {}
    response["x"] = x
    response["y"] = y
    return json.dumps(response)
Exemplo n.º 3
0
    def test_checkwinner(self):
        fake_board = [0, 0, 0, 0, 0,
                      1, 1, 1, 1, 0,
                      2, 2, 2, 2, 2,
                      0, 0, 0, 0, 0,
                      0, 0, 0, 0, 0]
        g = gomoku.Gomoku(fake_board, 5, 5)
        self.assertEqual(g.check_winner(), 2)

        fake_board[5 * 2 + 1] = 0
        g = gomoku.Gomoku(fake_board, 5, 5)
        g.size_x = 5
        g.size_y = 5
        self.assertEqual(g.check_winner(), 0)

        for i in range(0, 5):
            fake_board[i * 5 + i] = 1
        g = gomoku.Gomoku(fake_board, 5, 5)
        g.size_x = 5
        g.size_y = 5
        self.assertEqual(g.check_winner(), 1)
Exemplo n.º 4
0
    def run(self):
        self.client = client.MyClient(self.host, self.port, self.data_size)
        version = raw_input("Select game:\n 1 - Gomoku\n 2 - Guess number\n")
        response = self.client.sendMsg(version)
        if response == "good":
            print "good"
        else:
            print "wrong input"
            exit(1)

        if version == "1":
            size = raw_input("Give a size of grid (from 3 to 10): ")
            while self.client.sendMsg(size) != "good":
                size = raw_input("Wrong size, try again: ")
            game = gomoku.Gomoku(self.client, int(size))
        elif version == "2":
            game = guessWhat.GuessWhat(self.client)
        else:
            print "Wrong input"
            exit(1)
        game.run()
Exemplo n.º 5
0
    def run(self):
        self.server = server.MyServer(self.host, self.port, self.data_size)
        self.server.getConnection()

        version = self.server.getMsg()
        if version == "1":
            print "Play gomoku"
            self.server.connection.send("good")
            size = self.server.getMsg()
            while not 3 <= int(size) <= 10:
                self.server.connection.send("wrong")
                print "Wrong size"
                size = self.server.getMsg()
            self.server.connection.send("good")
            game = gomoku.Gomoku(self.server, int(size))
        elif version == "2":
            print "Play guessWhat"
            self.server.connection.send("good")
            game = guessWhat.GuessWhat(self.server, 150)
        else:
            self.server.connection.send("wrong")
            print "Wrong input"
            exit(1)
        game.run()
Exemplo n.º 6
0
def play(style,blacktype,whitetype):
#    if style == '3':
#        pygame.mixer.music.load('welcome.mp3')
#        pygame.mixer.music.play(loops=0, start=0.0)
#    pygame.mouse.set_visible(False)
    bwin = 0
    draw = 0
    wwin = 0
    print style,blacktype,whitetype
#    style = raw_input("Game Type: 1: HUMAN vs HUMAN\t 2: HUMAN vs AI\t\
#    3: AI vs HUMAN\t4: AI vs AI")
#    for _ in range(10):

    table = TABLE()
#    table.display()
#    AItype1 = raw_input("Input AItype: rand/value1")
#    AItype2 = raw_input("Input AItype: rand/value1")

    # 黑棋AI
    status = black
    deep = 0
    game = 0
    game = gomoku.Gomoku()
    game.draw()
    while True:
        if style == '1':
            x, y = moveByHuman(status, table, game)
        if style == '2':
            if status == black:
                x, y = moveByHuman(status, table, game)
            else:
                x, y = moveByAI(table, deep, status, game, whitetype)


        if style == '3':
            if status == white:
                x, y = moveByHuman(status, table, game)
            else:
                x, y = moveByAI(table, deep, status, game, blacktype)

        if style=='4':
            if status == white:
                x, y = moveByAI(table, deep, status, game, whitetype)

            else:
                x, y = moveByAI(table, deep, status, game, blacktype)



#        table.display()
        if table.judge(x, y, status):
            if status == black:
                bwin += 1

            else:
                wwin += 1
            print((bwin,wwin))
#            if style == '3':
#                pygame.mixer.music.load('victory.mp3')
#                pygame.mixer.music.play(loops=0, start=0.0)
#            table.display()
            time.sleep(3)
            pygame.mouse.set_visible(True)

            return
        elif table.is_full():
            draw += 1
#                table.display()
            pygame.mouse.set_visible(True)
            return
        else:
            #交换手
            status = -status
Exemplo n.º 7
0
 def __init__(self):
     super(Engine, self).__init__()
     self.resources = {}
     self.game = gomoku.Gomoku()
Exemplo n.º 8
0
def iswinner():
    data = request.json
    response = {}
    response["winner"] = gomoku.Gomoku(data["board"]).check_winner()
    return json.dumps(response)