米勒在九人莫里斯电脑对人游戏中的表现

2024-09-30 18:24:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在开发一个叫做九人莫里斯的游戏。我被困在轧机成型检查的一部分。我试过一段代码,但不起作用。我的意思是,问题是它无法识别玩家形成的磨坊。这是我为游戏试过的代码

# -*- coding: utf-8 -*-
"""
Created on Sat Sep 14 13:43:49 2019

@author: ravee
"""

import random


board = [' ' for i in range (25)]



def printBoard(board):

    print(board[1]+'-----------'+board[2]+'-----------'+board[3])
    print('|'+'                       '+'|')
    print('|'+'                       '+'|')
    print('|'+'   '+board[4]+'-------'+board[5]+'-------'+board[6]+'   '+'|')
    print('|'+'   '+'|'+'               '+'|'+'   '+'|')
    print('|'+'   '+'|'+'               '+'|'+'   '+'|')
    print('|'+'   '+'|'+'   '+board[7]+'---'+board[8]+'---'+board[9]+'   
    '+'|'+'   '+'|')
    print('|'+'   '+'|'+'   '+'|'+'       '+'|'+'   '+'|'+'   '+'|')
    print(board[10]+'   '+board[11]+'   '+board[12]+'       '+board[13]+'   
    '+board[14]+'   '+board[15])
    print('|'+'   '+'|'+'   '+'|'+'       '+'|'+'   '+'|'+'   '+'|')
    print('|'+'   '+'|'+'   '+board[16]+'---'+board[17]+'---'+board[18]+'   
    '+'|'+'   '+'|')
    print('|'+'   '+'|'+'               '+'|'+'   '+'|')
    print('|'+'   '+'|'+'               '+'|'+'   '+'|')
    print('|'+'   '+board[19]+'-------'+board[20]+'-------'+board[21]+'   
    '+'|')
    print('|'+'                       '+'|')
    print('|'+'                       '+'|')
    print(board[22]+'-----------'+board[23]+'-----------'+board[24])


def spaceIsFree(pos):

    return board[pos]==' '    

def playerMove():

    run =True
    while run:
        move=input('Please select the position at which you want to enter the 
        piece: ')
        try:
            move=int(move)
            if move>0 and move<25:
                if spaceIsFree(move):
                    run =False
                    insertPiece(move, 'B')
                    return move
                else:
                    print('Sorry, This space is occupied!.')
            else:
                print('Please insert the piece in the valid position')
        except:
            print('Please type a number')


def compMove():

    possibleMoves=[x for x, piece in enumerate(board) if piece==' ' and x!=0]
    move=0

    for let in ['A','B']:
        for i in possibleMoves:
            boardCopy=board[:]
            boardCopy[i]=let
            if isMill(boardCopy,let):
                move=i
                return move

    cornersOpen=[]
    for i in possibleMoves:
        if i in [1,3,4,6,7,9,16,18,19,21,22,24]:
            cornersOpen.append(i)
    if(len(cornersOpen)>0):
        move=selectRandom(cornersOpen)
        return move

    edgesOpen=[]
    for i in possibleMoves:
        if i in [2,5,8,10,11,12,13,14,15,17,20,23]:
            edgesOpen.append(i)
    if(len(edgesOpen)>0):
        move=selectRandom(edgesOpen)
        return move

def insertPiece(pos,piece):

    board[pos]=piece

def removePieceP(pos):

    board.pop(pos)
    board.insert(pos,' ')
    printBoard(board)

def removePieceC():

    possibleRemoves=[x for x, piece in enumerate(board) if piece=='B']

    ln=len(possibleRemoves)
    remove=random.randrange(0,ln)
    return remove
    board[remove]=' '



def movePiece(pos1,pos2):

    board[pos2]=board[pos1]
    board[pos1]==' '


def isMill(bo,le):

    mills=[]
    listmill=[(1,2,3),(4,5,6),(7,8,9),(10,11,12),(13,14,15),(16,17,18), 
    (19,20,21),(22,23,24),(1,10,22),(4,11,19),(7,12,16),(2,5,8),(17,20,23), 
    (9,13,18),(6,14,21),(3,15,24)]
    for i in listmill:
        for j in i:
            if bo[j]==le and bo[j+1]==le and bo[j+2]==le and (j,j+1,j+2) in 
    listmill:
                mills.append((j,j+1,j+2))
            break;

    print(mills)
    for i in mills:
        if i in listmill:
            return True
            print( i)
            return i
            break;



def isWinner(board,piece):

    countA=0
    countB=0
    for i in board:
        if i=='A':
            countA=countA+1
        elif i=='B':
            countB=countB+1

    if countB>2 and countA==2:
        print("Good Job. You won the game!")
    if countA>2 and countB==2:
        print("Sorry, Computer won this time. Better luck next time!")


def selectRandom(li):

    ln=len(li)
    r=random.randrange(0,ln)
    return li[r]




def main():

    print('Welcome to Nine Mens Morris!')
    printBoard(board)
    print("Computer is 'A' and you are 'B'")

    for nine in range(0,9):    

        playerMove()
        printBoard(board)

        if isMill(board,'B'):
            p1=int(input('You have formed a mill. Remove any A piece: '))
            removePieceP(p1)




        if not(isWinner(board,'A')):
            move=compMove()
            insertPiece(move,'A')
            printBoard(board)
            print('Computer placed an "A" at position',move)
            if isMill(board,'A'):
                removePieceC()
                print('Computer has formed a mill. Your piece B is removed 
    from position' ,p1)

        else:
            removePieceC()
            print('You has formed a mill. Your piece B is removed from 
    position ,p1')






main()    

我期望isMill()函数获取所形成的mill,并在main函数中显示mill所形成的板的索引


Tags: andtheinposboardforpiecemove