使用python制作一个带有人工智能的石头剪子布游戏

最近一直在学习python,突发奇想,可以做一个人工智能程序。但别的先不提,光做一个语音识别我都不会做呀!![痛哭流涕],我便只能做一个石头剪子布来安慰一下我受伤的心灵…………
制作带有人工智能的石头剪子布程序
第一步:初始化所需的变量

import random
pattern = [[0,0,0,0],[0,0,0,0]]
unif = ['2A 0D CF 23','33 A1 DB 7C','39 E4 AF 00 1A','5A B4 0F 60','E8 4F BC A9']
ying_ping_shu = [0,0,0]
lose = 0
p = 0
n = 0
human = 0
computer = 0

第二步:因为这是一个带有学习功能的程序,所以要在前面先“试试手”通过学习来获取玩家的特点

for i in range(4):
     print('0石头1剪刀2布')
     try:
          human = input('')
          if human == 'list':
               human = 0
               print('赢:'+str(ying_ping_shu[0])+'平:'+str(ying_ping_shu[1])+'输:'+str(ying_ping_shu[2]))
          if human == 'exit':
               exit()
          else:
               human = int(human)
          pattern[0][i] = human
          print('正在学习: PL:'+str(pattern)+' lose:'+str(lose)+' pea:'+str(p)+' nea:'+str(n)+' player:'+str(human)+' UNIF:'+str(unif[i+random.randint(0,1)]))
     except:
          print('Error!')
          human = 0
     if human < 0 or human >2:
          print('Error!')
          human = 0
     if pattern[p][n] ==0:
          computer = 2
     if pattern[p][n] ==1:
          computer = 1
     if pattern[p][n] ==2:
          computer = 1
     n = (n + 1) % 4
     if computer == 0:
          print('电脑石头')
     elif computer ==1:
          print('电脑剪刀')
     else:
          print('电脑布')
     if computer < human:
          print('电脑胜!')
          ying_ping_shu[2] += 1
     elif computer == 2 and human == 0:
          print('电脑胜!')
          ying_ping_shu[2] += 1
     elif computer == human:
          print('平局!')
          ying_ping_shu[1] += 1
     else:
          print('您赢了!')
          ying_ping_shu[0] += 1
     print('\n')
     if ((human == 0 and computer == 1) or (human == 1 and computer == 2) or (human == 2 and computer == 3)):
          lose += 1
     else:
          lose = 0
     if lose >= 2:
          p = (p + 1) % 2
          n = 0
for i in range(4):
     print('0石头1剪刀2布')
     try:
          human = input('')
          if human == 'list':
               human = 0
               print('赢:'+str(ying_ping_shu[0])+'平:'+str(ying_ping_shu[1])+'输:'+str(ying_ping_shu[2]))
          if human == 'exit':
               exit()
          else:
               human = int(human)
          pattern[1][i] = human
          print('正在学习: PL:'+str(pattern)+' lose:'+str(lose)+' pea:'+str(p)+' nea:'+str(n)+' player:'+str(human)+' UNIF:'+str(unif[i+random.randint(0,1)]))
     except:
          print('Error!')
          human = 0
     if human < 0 or human >2:
          print('Error!')
          human = 0
     if pattern[p][n] ==0:
          computer = 2
     if pattern[p][n] ==1:
          computer = 0
     if pattern[p][n] ==2:
          computer = 1
     n = (n + 1) % 4
     if computer == 0:
          print('电脑石头')
     elif computer ==1:
          print('电脑剪刀')
     else:
          print('电脑布')
     if computer < human:
          print('电脑胜!')
          ying_ping_shu[2] += 1
     elif computer == 2 and human == 0:
          print('电脑胜!')
          ying_ping_shu[2] += 1
     elif computer == human:
          print('平局!')
          ying_ping_shu[1] += 1
     else:
          print('您赢了!')
          ying_ping_shu[0] += 1
     print('\n')
     if ((human == 0 and computer == 1) or (human == 1 and computer == 2) or (human == 2 and computer == 3)):
          lose += 1
     else:
          lose = 0
     if lose >= 2:
          p = (p + 1) % 2
          n = 0

之所以用两段代码,是因为这是两个不同的学习过程:第一段是随机数出拳来试探对方,第二段是随机数略加一点电脑自己的推断。
第三步:电脑正式“进入状态”,通过最初获得的经验和玩家的出拳来出拳:

while True:
     print('0石头1剪刀2布')
     try:
          human = input('')
          if human == 'list':
               human = 0
               print('赢:'+str(ying_ping_shu[0])+'平:'+str(ying_ping_shu[1])+'输:'+str(ying_ping_shu[2]))
          if human == 'exit':
               exit()
          else:
               human = int(human)
     except:
          print('Error!')
          human = 0
     if human < 0 or human >2:
          print('Error!')
          human = 0
     if pattern[p][n] ==0:
          computer = 2
     if pattern[p][n] ==1:
          computer = 0
     if pattern[p][n] ==2:
          computer = 1
     print('正在出拳: PL:'+str(pattern)+' lose:'+str(lose)+' pea:'+str(p)+' nea:'+str(n)+' player:'+str(human)+' computer:'+str(computer)+' UNIF:'+str(unif[i+random.randint(0,1)]))
     n = (n + 1) % 4
     if computer == 0:
          print('电脑石头')
     elif computer ==1:
          print('电脑剪刀')
     else:
          print('电脑布')
     if computer < human:
          print('电脑胜!')
          ying_ping_shu[2] += 1
     elif computer == 2 and human == 0:
          print('电脑胜!')
          ying_ping_shu[2] += 1
     elif computer == human:
          print('平局!')
          ying_ping_shu[1] += 1
     else:
          print('您赢了!')
          ying_ping_shu[0] += 1
     print('\n')
     if ((human == 0 and computer == 1) or (human == 1 and computer == 2) or (human == 2 and computer == 3)):
          lose += 1
     else:
          lose = 0
     if lose >= 2:
          p = (p + 1) % 2
          n = 0

效果:
使用python制作一个带有人工智能的石头剪子布游戏_第1张图片
至此整个程序就做完了,从效果图中可以看出电脑变得非常强,似乎可以预测玩家的出拳。
这就是我制作的带有人工智能的石头剪子布游戏,希望大家多多指教!

你可能感兴趣的:(python,人工智能,python,人工智能,机器学习)