Ejemplo n.º 1
0
def train():  ##训练,AI与AI下
    win = gobangwin()

    for i in range(COLUMN + 1):
        for j in range(ROW + 1):
            list_all.append((i, j))

    change = 0
    state = 0  ##state=0表示游戏进行
    m = 0
    n = 0
    global lastpiece

    while state == 0:
        if change == 0:
            # win.getMouse()
            pos = (random2.randrange(8, 9), random2.randrange(8, 9))
            list2.append(pos)
            list3.append(pos)
            piece = Circle(Point(GRID_WIDTH * pos[0], GRID_WIDTH * pos[1]), 16)
            piece.setFill('black')

            piece.draw(win)
            change = change + 1

        elif change == 1:
            # win.getMouse()
            pos = (random2.randrange(6, 9), random2.randrange(5, 8))
            while pos == list2[0]:  ##避免随机点与黑子的第一个点相同
                pos = (random2.randrange(7, 9), random2.randrange(7, 10))
            list1.append(pos)
            list3.append(pos)
            piece = Circle(Point(GRID_WIDTH * pos[0], GRID_WIDTH * pos[1]), 16)
            piece.setFill('red')
            global lastpiece
            lastpiece = piece

            piece.draw(win)
            change = change + 1

        elif change % 2 == 1 and change > 2:  ##change为单数则轮到AI下
            pos = ai()
            if pos in list3:
                message = Text(Point(200, 200),
                               "不可用的位置" + str(pos[0]) + "," + str(pos[1]))
                message.draw(win)
                state = 1

            list1.append(pos)
            list3.append(pos)

            piece = Circle(Point(GRID_WIDTH * pos[0], GRID_WIDTH * pos[1]), 16)
            piece.setFill('Red')

            if change > 2:
                lastpiece.setFill('white')
                lastpiece = piece
            lastpiece = piece
            piece.draw(win)

            if game_win(list1):
                message = Text(Point(100, 100), "Winner: White")
                message.draw(win)
                state = 1  ##AI赢了,state=1 游戏结束
                evaluationX(0)
            change = change + 1

        else:
            # win.getMouse()
            a2, b2 = anotherAI()
            list2.append((a2, b2))
            list3.append((a2, b2))
            piece = Circle(Point(GRID_WIDTH * a2, GRID_WIDTH * b2), 16)

            piece.setFill('black')
            piece.draw(win)
            if game_win(list2):
                message = Text(Point(100, 100), "Winner: Black")
                message.draw(win)  #AI输了,要进行训练
                evaluationX(1)
                state = 1
                workbook = xlsxwriter.Workbook(
                    r'D:\107工程\大创\gobang_AI-master\score.xlsx')
                newsheet = workbook.add_worksheet()
                for i in range(0, len(score)):
                    newsheet.write(i, 0, score[i] * 2 / (usageSheet[i] + 1))
                workbook.close()

            change = change + 1

    message = Text(Point(100, 120), "Click anywhere to quit.")
    message.draw(win)
    win.getMouse()
    win.close()
Ejemplo n.º 2
0
def main():
    win = gobangwin()

    for i in range(COLUMN + 1):
        for j in range(ROW + 1):
            list_all.append((i, j))

    change = 0
    state = 0  ##state=0表示游戏进行
    m = 0
    n = 0
    global lastpiece

    while state == 0:
        if change == 1:  #AI的第一步在棋盘中间随机取点下,避免开局一成不变
            # win.getMouse()
            pos = (random2.randrange(8, 9), random2.randrange(8, 9))
            while pos == list2[0]:  ##避免随机点与玩家的第一个点相同
                pos = (random2.randrange(4, 12), random2.randrange(4, 12))
            # print(list2[0])
            # print(pos)
            list1.append(pos)
            list3.append(pos)
            piece = Circle(Point(GRID_WIDTH * pos[0], GRID_WIDTH * pos[1]), 16)
            piece.setFill('Red')
            global lastpiece
            lastpiece = piece
            piece.draw(win)
            change = change + 1

        elif change % 2 == 1 and change > 2:  ##change为单数则轮到AI下
            pos = ai()  ##获得AI判断的下一步
            if pos in list3:
                message = Text(Point(200, 200),
                               "不可用的位置" + str(pos[0]) + "," + str(pos[1]))
                message.draw(win)
                state = 1

            list1.append(pos)
            list3.append(pos)

            piece = Circle(Point(GRID_WIDTH * pos[0], GRID_WIDTH * pos[1]), 16)
            piece.setFill('Red')

            if change > 2:
                lastpiece.setFill('white')
            lastpiece = piece
            piece.draw(win)

            if game_win(list1):
                message = Text(Point(100, 100), "Winner: White")
                message.draw(win)
                state = 1  ##AI赢了,state=1 游戏结束
            change = change + 1

        else:  ##轮到玩家下
            p2 = win.getMouse()
            if not ((round(
                (p2.getX()) / GRID_WIDTH), round(
                    (p2.getY()) / GRID_WIDTH)) in list3):

                a2 = round((p2.getX()) / GRID_WIDTH)
                b2 = round((p2.getY()) / GRID_WIDTH)
                list2.append((a2, b2))
                list3.append((a2, b2))
                piece = Circle(Point(GRID_WIDTH * a2, GRID_WIDTH * b2), 16)

                piece.setFill('black')
                piece.draw(win)
                if game_win(list2):
                    message = Text(Point(100, 100), "Winner: Black")
                    message.draw(win)
                    state = 1

                change = change + 1

    message = Text(Point(100, 120), "Click anywhere to quit.")
    message.draw(win)
    win.getMouse()
    win.close()
Ejemplo n.º 3
0
from random2 import randrange
parametro_inicial = 1
parametro_final = 30
numero_aleatorio = randrange(parametro_inicial, parametro_final)
intentos = 1

manejador_bucle = True
print('ADIVINA EL NUMERO ENTRE {} y {}'.format(parametro_inicial,
                                               parametro_final))
print('INTENTOS: {}\n'.format(intentos))

while manejador_bucle:
    # print(numero_aleatorio)

    if (intentos != 1):
        print('INTENTOS: {}'.format(intentos))

    numero = input('Ingrese un numero:\n')
    numero = int(numero)

    if (numero == numero_aleatorio):
        print(
            'FELICIDADES HAZ GANADO EL NUMERO ES: {}'.format(numero_aleatorio))
        print('LO HAZ CONSEGUIDO EN {} INTENTOS'.format(intentos))

        seguir = input(
            '**Deseas seguir jugando s/n. Si presionas cualquier otro carater se tomara como si.**\n'
        )

        if (seguir == 's'):
            manejador_bucle = True
Ejemplo n.º 4
0
from asyncore import dispatcher

from random2 import randrange

# program simulates rolled dice for rpg games

# defined terms

d3_1 = randrange(1, 4)
d3_2 = randrange(1, 4)
d3_3 = randrange(1, 4)
d3_4 = randrange(1, 4)
d3_total2 = d3_1 + d3_2
d3_total3 = d3_1 + d3_2 + d3_3
d3_total4 = d3_1 + d3_2 + d3_3 + d3_4
d4_1 = randrange(1, 5)
d4_2 = randrange(1, 5)
d4_3 = randrange(1, 5)
d4_4 = randrange(1, 5)
d4_total2 = d4_1 + d4_2
d4_total3 = d4_1 + d4_2 + d4_3
d4_total4 = d4_1 + d4_2 + d4_3 + d4_4
d6_1 = randrange(1, 7)
d6_2 = randrange(1, 7)
d6_3 = randrange(1, 7)
d6_4 = randrange(1, 7)
d6_total2 = d6_1 + d6_2
d6_total3 = d6_1 + d6_2 + d6_3
d6_total4 = d6_1 + d6_2 + d6_3 + d6_4
d8_1 = randrange(1, 9)
d8_2 = randrange(1, 9)
Ejemplo n.º 5
0
from random2 import randrange

# Code creates a guessing game where the computer guesses a number and gives the user hints. Can produce any number between 1  - 100 and returns a high score of 100.

# Definitions

random_number = randrange(1, 101)
compare_number = randrange(2, 11)
score = 100
print_score = ('Score = ' + str(score))
hint_generator_1 = randrange(1, 4)
hint_generator_2 = randrange(1, 4)
hint_generator_3 = randrange(1, 4)
hint_generator_4 = randrange(1, 4)
hint_generator_5 = randrange(1, 4)
hint_generator_6 = randrange(1, 4)
hint_generator_7 = randrange(1, 4)
hint_generator_8 = randrange(1, 4)
hint_generator_9 = randrange(1, 4)
hint_generator_10 = randrange(1, 4)

# functions for hints. A random type of hint will be given for each wrong answer.


def higher_or_lower_1(input_1, random_number):
    if input_1 > random_number:
        print('Guess lower')
    if input_1 < random_number:
        print('Guess higher')