1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 | import random def drawBoard(board) : #이 함수는 파라미터로 받은 보드를 출력 #board는 10개의 문자열로 된 리스트 이며 보드를 나타낸다.(인덱스 0은 무시) print(' | |') print(' '+board[7]+' | '+board[8]+' | '+board[9]) print(' | |') print('-------------------') print(' | |') print(' '+board[4]+' | '+board[5]+' | '+board[6]) print(' | |') print('-------------------') print(' | |') print(' '+board[1]+' | '+board[2]+' | '+board[3]) print(' | |') def inputPlayerLetter(): #플레이어가 어떤 글자 마크로 할 것인지 선택 #플레이어가 선택한 글자를 첫 번째 아이템으로, 컴퓨터의 문자를 두 번째 아이템으로 하는 문자열 반환 letter = ' ' while not (letter == 'X' or letter == 'O'): print('Do you want to be X or O?') letter = input().upper() #투플의 첫 번째 요소가 플레이어 글자, 두 번째 요소는 컴퓨터 글자 if letter == 'X': return['X', 'O'] else : return['O', 'X'] def whoGoesFirst(): #누가 먼저 할 것인지 임의로 정한다. if random.randint(0,1) == 0: return 'computer' else : return 'player' def playAgain(): #플레이어가 또 게임을 하겠다면 True반환, 아니라면 False 반환 print('Do you want to play again? (yes or no)') return input().lower().startswith('y') def makeMove(board, letter, move): board[move] = letter def isWinner(bo, le): #보드와 플레이어의 글자를 주면, 플레이어가 이겼을때 True 반환 return ((bo[7] == le and bo[8] == le and bo[9] == le) or #윗부분을 가로지르는지 (bo[4] == le and bo[5] == le and bo[6] == le) or #중간을 가로지르는지 (bo[1] == le and bo[2] == le and bo[3] == le) or #아랫부분을 가로지르는지 (bo[7] == le and bo[4] == le and bo[1] == le) or #왼쪽 가장자리에서 세로로 내려오는지 (bo[8] == le and bo[5] == le and bo[2] == le) or #가운데에서 세로로 내려오느지 (bo[9] == le and bo[6] == le and bo[3] == le) or #오른쪽 가장자리에서 세로로 내려오는지 (bo[7] == le and bo[5] == le and bo[3] == le) or #대각선 (bo[9] == le and bo[5] == le and bo[1] == le))#대각선 def getBoardCopy(board): #보드 리스트를 복제한 다음 복제한 리스트를 반환 dupeBoard = [] for i in board: dupeBoard.append(i) return dupeBoard def isSpaceFree(board, move): #보드에서 move 위치가 비어 있으면 True를 반환 return board[move] == ' ' def getPlayerMove(board): #플레이어가 움직일 위치를 입력하도록 한다. move = ' ' while move not in '1 2 3 4 5 6 7 8 9'.split() or not isSpaceFree(board, int(move)): print('What is your next move? (1-9)') move = input() return int(move) def chooseRandomMoveFromList(board, movesList): #moveList와 board를 보고 가능한 위치를 반환 #가능한 위치가 하나도 없으면 None을 반환 possibleMoves = [] for i in movesList: if isSpaceFree(board, i): possibleMoves.append(i) if len(possibleMoves) != 0: return random.choice(possibleMoves) else: return None def getComputerMove(board, computerLetter): if computerLetter == 'X': playerLetter = 'O' else : playerLetter = 'X' #틱택토 인공지능 알고리즘 #우선 다음에 이길 수 있는 검사 for i in range(1, 10): copy = getBoardCopy(board) if isSpaceFree(copy, i): makeMove(copy, computerLetter, i) if isWinner(copy, computerLetter): return i #상대편이 다음 번엔 이길 수 잇는지 검사해서 그렇다면 막음 for i in range(1, 10): copy = getBoardCopy(board) if isSpaceFree(copy, i): makeMove(copy, playerLetter, i) if isWinner(copy, playerLetter): return i #만약 비어 있으면 코너 차단 move = chooseRandomMoveFromList(board, [1, 3, 7, 9]) if move != None: return move #만약 비어 있으면 중앙 차지한다. if isSpaceFree(board,5): return 5 #한쪽 면으로 이동한다. return chooseRandomMoveFromList(board, [2, 4, 6, 8]) def isBoardFull(board): #보드의 모든 공간이 다 찼으면 True를 반환, 그렇지 않으면 False를 반환 for i in range(1, 10): if isSpaceFree(board, i): return False return True print('Welcome to Tic Tac Toe!') while True: #보드를 재설정한다. theBoard = [' '] *10 playerLetter, computerLetter = inputPlayerLetter() turn = whoGoesFirst() print('The '+turn+'will go first.') gameIsPlaying = True while gameIsPlaying: if turn == 'player': #플레이어 차례 drawBoard(theBoard) move = getPlayerMove(theBoard) makeMove(theBoard, playerLetter, move) if isWinner(theBoard, playerLetter): drawBoard(theBoard) print('Hooray! You have won the game!') gameIsPlaying = False else: if isBoardFull(theBoard): drawBoard(theBoard) print('The game is a tie!') break else: turn = 'computer' else: #컴퓨터의 차례 move = getComputerMove(theBoard, computerLetter) makeMove(theBoard, computerLetter, move) if isWinner(theBoard, computerLetter): drawBoard(theBoard) print('The computer has beaten you! You lose.') gameIsPlaying = False else: if isBoardFull(theBoard): drawBoard(theBoard) print('The game is a tie!') break else: turn = 'player' if not playAgain(): break | cs |
반응형
'프로그래밍' 카테고리의 다른 글
| arduino pot_manger.ppt (0) | 2015.08.22 |
|---|---|
| 아두이노 3일차 정부지원교육 (0) | 2015.07.10 |
| Python_hangman (0) | 2015.07.01 |
| Python dragon_cave (0) | 2015.06.30 |
| Python 숫자 맞추기 게임 (0) | 2015.06.30 |