B-Ship:交替AI和用户(Python)

时间:2015-02-23 04:54:25

标签: python python-3.x

我正在制作b-ship游戏。我试图让用户点击ai,然后是用户,然后是ai等。我该如何管理?

import random
import time

def drawboard(hitboard,hitboard2):
    print(' Opponent\'s          Your')
    print('    Ships            Ships')
    print('|   |   |   |    |   |   |   |')
    print('| ' + hitboard[7] + ' | ' + hitboard[8] + ' | ' + hitboard[9] + ' |    | ' + hitboard2[7] + ' | ' + hitboard2[8] + ' | ' + hitboard2[9] + ' |')
    print('|   |   |   |    |   |   |   |')
    print('-------------    -------------')
    print('|   |   |   |    |   |   |   |')
    print('| ' + hitboard[4] + ' | ' + hitboard[5] + ' | ' + hitboard[6] + ' |    | ' + hitboard2[4] + ' | ' + hitboard2[5] + ' | ' + hitboard2[6] + ' |')
    print('|   |   |   |    |   |   |   |')
    print('-------------    -------------')
    print('|   |   |   |    |   |   |   |')
    print('| ' + hitboard[1] + ' | ' + hitboard[2] + ' | ' + hitboard[3] + ' |    | ' + hitboard2[1] + ' | ' + hitboard2[2] + ' | ' + hitboard2[3] + ' |')
    print('|   |   |   |    |   |   |   |')
def aiships(hitboard,spot_hit,shipspots,hitboard2):
    if spot_hit in shipspots:
        hitboard[spot_hit] = 'x'
    else:
        hitboard[spot_hit] = 'o'
    drawboard(hitboard,hitboard2)

def middle(spot_hit, hitboard2):
    if spot_hit in usership:
        pass

def playerships(hitboard,hitboard2, x, usershipspots):
    drawboard(hitboard,hitboard2)
    time.sleep(0.5)
    print("\nComputer's turn.\n")
    time.sleep(0.5)
    if x in usershipspots:
        hitboard2[x] =='x'
        hitboard2[usershipspots[1]] = 'x'
        drawboard(hitboard,hitboard2)        
        return True #AI wins
    else:
        hitboard2[x] == 'o'
        drawboard(hitboard,hitboard2)
        return False #Player wins

def main():
    twoships = [[1,2], [2,1], [1,4], [4,1], [2,3], [3,2], [2,5], [5,2], [3,6], [6,3], [4,5], [5,4], [4,7], [7,4], [5,6], [6,5], [5,8], [8,5], [6,9], [9,6], [7,8], [8,7], [8,9], [9,8]]
    threeships = [[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,2,1], [3,1,2], [1,4,7], [1,7,4], [4,1,7], [4,7,1], [7,4,1], [7,1,4], [2,5,8], [2,8,5], [5,8,2], [5,2,8], [8,5,2], [8,2,5], [3,6,9], [3,9,6], [6,9,3], [6,3,9], [9,6,3], [9,3,6], [4,5,6], [4,6,5], [5,6,4], [5,4,6], [6,5,4], [6,4,5], [7,8,9], [7,9,8], [9,8,7], [9,7,8], [8,7,9], [8,9,7]]
    shipspots1 = random.choice(twoships)
    shipspots2 = random.choice(threeships)

    gameisplaying = True
    while gameisplaying:
        hitboard = [' ' for i in range(10)]
        hitboard2 = [' ' for i in range(10)]
        ship1sp1 = 'true'
        ship1sp2 = 'true'
        ship2sp1 = 'true'
        ship2sp2 = 'true'
        ship3sp2 = 'true'
        while ship1sp1 == 'true':
            userships = []
            sh1sp1 = int(input('What is the first space of your first ship (2 spaces)?'))
            while sh1sp1 not in '1 2 3 4 5 6 7 8 9'.split():
                sh1sp1 = int(input('What is the first space of your first ship (2 spaces)?'))
            userships.append(sh1sp1)
            hitboard2[sh1sp1] = 's'
            ship1sp1 = 'false'

        while ship1sp2 == 'true':
            sh1sp2 = int(input('What is the second space of your first ship (2 spaces)?'))
            while sh1sp2 not in '1 2 3 4 5 6 7 8 9'.split():
                sh1sp2 = int(input('What is the second space of your first ship (2 spaces)?'))
            userships.append(sh1sp2)
            hitboard2[sh1sp2] = 's'
            ship1sp2 = 'false'

        while ship2sp1 == 'true':
            sh2sp1 = int(input('What is the first space of your second ship (2 spaces)?'))
            while sh2sp1 not in '1 2 3 4 5 6 7 8 9'.split():
                sh2sp1 = int(input('What is the first space of your second ship (2 spaces)?'))
            userships.append(sh2sp1)
            hitboard2[sh2sp1] = 's'
            ship2sp1 = 'false'

        while ship2sp2 == 'true':
            sh2sp2 = int(input('What is the second space of your second ship (2 spaces)?'))
            while sh2sp2 not in '1 2 3 4 5 6 7 8 9'.split():
                sh2sp2 = int(input('What is the second space of your first ship (2 spaces)?'))
            userships.append(sh2sp2)
            hitboard2[sh2sp2] = 's'
            ship2sp2 = 'false'

        while ship2sp3 == 'true':
            sh2sp3 = int(input('What is the third space of your second ship (2 spaces)?'))
            while sh2sp3 not in '1 2 3 4 5 6 7 8 9'.split():
                sh2sp3 = int(input('What is the third space of your first ship (2 spaces)?'))
            userships.append(sh2sp3)
            hitboard2[sh2sp3] = 's'
            ship2sp3 = 'false'

        aiturn=True
        while aiturn:
            spot_hit = input('Where\'s the hit?: 1-9  ')
            while not (spot_hit in '1 2 3 4 5 6 7 8 9'.split()):
                spot_hit = input ('Please tell me where the hit is: 1-9  (or Ctrl/Command+C to quit) ')
            spot_hit = int(spot_hit)
            aiships(hitboard, spot_hit, shipspots, hitboard2)
            if 1 == 1:
                gameisplaying = False
                print("PLAYER WINS")

        ai_index += 1

main()

修改 我添加了错误检查,但它似乎在无限循环中运行。有什么问题?

1 个答案:

答案 0 :(得分:1)

您需要将应用程序拆分为两个循环;你把它们组合在一起,这是你混乱的根源。

第一个循环用于设置玩家的船只。这是一个无限循环,直到所有船只都正确放置。

使用计数器启动循环,这是最大数量的船只。

这是一个如何运作的想法

user_ships = []  # this will store the ship's coordinates
total_ships = 2 # How many ships a user can have

while len(user_ships) < total_ships:
    coordinates = input('Please enter your ship coordinates: ')
    if check_if_coordinates_are_valid(coordinates):
        if check_if_spot_is_free(coordinates):
            user_ships.append(coordinates)
        else:
            print('Sorry, that spot is already taken by your ship')
    else:
        print('Sorry, those are not valid coordinates')

接下来,让计算机随机放置自己的船只;和它一样的想法 用户的循环,而不是打印出任何错误消息,只是通过随机选择,直到找到合适的坐标:

computer_ships = []
while len(computer_ships) < total_ships:
    coordinates = random.choice(grid)
    if check_if_spot_is_free(coordinates):
        computer_ships.append(coordinates)

一旦您收集了两个用户的船只 - 将它们存储在字典中以便日后访问:

game_board = {} # an empty dictionary
game_board['player'] = user_ships
game_board['computer'] = computer_ships

现在你拥有了所有的船只,你可以开始你的游戏循环。这个循环的条件是用户没有沉没对方的舰队。

您将在这里使用的方法很少。第一个是检查目标是否击中了对手的船只:

def check_if_ship_hit(target, user, game_board):
    ships = game_board[user]
    if target not in ships:
        return False, game_board
    else:
        game_board[user][target] = None # Sunk!
        return True, game_board
    return False, game_board

第二个将检查用户是否丢失:

def user_lost(user, game_board):
    ships = game_board[user]
    # If the user has all Nones in his
    # board, he loses
    return sum(1 for i in ships if not i) == len(ships)

一旦我们拥有了这些,现在我们可以开始游戏了:

player_wins = False
computer_wins = False

while player_wins != True or computer_wins != True:
    # First, its the player's turn:
    target = input('Player, enter a coordinate for the enemy ship: ')
    result, game_board = check_if_ship_hit(target, user='computer', game_board)
    if result:
       print('You scored a hit!')
    else:
       print('You miss!')
    player_wins = user_lost('computer', game_board)

    # Now, the computer's turn
    target = random.choice(grid)
    print('Computer selected a target!')
    result, game_board = check_if_ship_hit(target, user='player', game_board)
    if result:
        print('You are hit!')
    else:
        print('Computer misses!')
    computer_wins = user_lost('player', game_board)