{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# AI Warmup Exercise: Tic-Tac-Toe"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Defining a Board class"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"class TTTBoard:\n",
" def __init__(self):\n",
" self.board = [' '] * 9\n",
" \n",
" def __getitem__(self, pos):\n",
" return self.board[pos]\n",
"\n",
" def __setitem__(self, pos, piece):\n",
" if self.board[pos] == ' ':\n",
" self.board[pos] = piece\n",
" else:\n",
" raise Exception('Invalid move: {} at {}'.format(piece, pos))\n",
" \n",
" def gameover(self):\n",
" if all(p != ' ' for p in self.board):\n",
" return True\n",
" for line in ((0,1,2), (3,4,5), (6,7,8), (0,3,6), \n",
" (1,4,7), (2,5,8), (0,4,8), (2,4,6)):\n",
" if (all(self.board[i] == 'X' for i in line) or\n",
" all(self.board[i] == 'O' for i in line)):\n",
" return True\n",
" return False\n",
" \n",
" def __repr__(self):\n",
" s = ''\n",
" for i in range(0, 9, 3):\n",
" s += ' | '.join(self.board[i:i+3])\n",
" s += '\\n'\n",
" if i < 6:\n",
" s += '-' * 9 + '\\n'\n",
" return s"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"b = TTTBoard()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"## Random move functions"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import random\n",
"\n",
"def randpos(board):\n",
" return random.choice([i for i in range(9) if board[i] == ' '])\n",
"\n",
"def randcornerpos(board):\n",
" try:\n",
" return random.choice([i for i in (0, 2, 6, 8) if board[i] == ' '])\n",
" except:\n",
" return -1"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"randpos(b)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Player functions"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# A: Vincent Tran"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def player1_a(move, board):\n",
" global secondMove, blocked, secondBlock, thirdPos, thirdMove, fourthMove\n",
" if move == 0:\n",
" board[6] = 'X'\n",
" elif move == 2:\n",
" for i in range(9):\n",
" if board[i] == 'O':\n",
" blocked = i\n",
" if blocked == 0 or blocked == 2 or blocked == 3 or blocked == 5:\n",
" secondMove = 8\n",
" board[secondMove] = 'X'\n",
" elif blocked == 1 or blocked == 7 or blocked == 8:\n",
" secondMove = 0\n",
" board[secondMove] = 'X'\n",
" else:\n",
" secondMove = 2\n",
" board[secondMove] = 'X'\n",
" elif move == 4:\n",
" thirdMove = []\n",
" if (secondMove == 0 and board[3] == ' '):\n",
" board[3] = 'X'\n",
" elif (secondMove == 8 and board[7] == ' '):\n",
" board[7] = 'X'\n",
" elif (blocked == 0 or blocked == 3) and secondMove == 8:\n",
" thirdPos = 2\n",
" board[thirdPos] = 'X'\n",
" thirdMove.append(5)\n",
" thirdMove.append(3)\n",
" elif (blocked == 2 or blocked == 5) and secondMove == 8:\n",
" thirdPos = 0\n",
" board[thirdPos] = 'X'\n",
" thirdMove.append(3)\n",
" thirdMove.append(7)\n",
" elif (blocked == 1 or blocked == 2) and secondMove == 0:\n",
" thirdPos = 8\n",
" board[thirdPos] = 'X'\n",
" thirdMove.append(3)\n",
" thirdMove.append(7)\n",
" elif (blocked == 7 or blocked == 8) and secondMove == 0:\n",
" thirdPos = 2\n",
" board[thirdPos] = 'X'\n",
" thirdMove.append(1)\n",
" thirdMove.append(3)\n",
" elif (board[1] == 'O' and board[7] == 'O') or (board[3] == 'O' and board[5] == 'O'):\n",
" thirdPos = 4\n",
" board[thirdPos] = 'X'\n",
" elif blocked == 4:\n",
" for i in (0,1,3,5,7,8):\n",
" if board[i] == 'O':\n",
" secondBlock = i\n",
" if secondBlock == 1:\n",
" board[7] = 'X'\n",
" thirdMove.append(8)\n",
" thirdMove.append(0)\n",
" elif secondBlock == 7:\n",
" board[1] = 'X'\n",
" thirdMove.append(0)\n",
" thirdMove.append(8)\n",
" elif secondBlock == 3:\n",
" board[5] = 'X'\n",
" thirdMove.append(8)\n",
" thirdMove.append(0)\n",
" elif secondBlock == 5:\n",
" board[3] = 'X'\n",
" thirdMove.append(0)\n",
" thirdMove.append(8)\n",
" elif secondBlock == 0:\n",
" board[8] = 'X'\n",
" thirdMove.append(7)\n",
" thirdMove.append(5)\n",
" elif secondBlock == 8:\n",
" board[0] = 'X'\n",
" thirdMove.append(1)\n",
" thirdMove.append(3) \n",
" elif move == 6:\n",
" if board[4] == ' ':\n",
" board[4] = 'X'\n",
" else:\n",
" for i in thirdMove:\n",
" if board[i] == ' ':\n",
" board[i] = 'X'\n",
" break \n",
" elif move == 8:\n",
" board[randpos(board)] = 'X' "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def player2_a(move, board):\n",
" global firstMove\n",
" if move == 1:\n",
" if board[4] == ' ':\n",
" firstMove = 4\n",
" board[firstMove] = 'O'\n",
" else:\n",
" firstMove = 0\n",
" board[firstMove] = 'O'\n",
" elif move == 3:\n",
" if firstMove == 4:\n",
" if ((board[0] == 'X' and board[1] == 'X') or \n",
" (board[5] == 'X' and board[8] == 'X')):\n",
" board[2] = 'O'\n",
" elif ((board[0] == 'X' and board[3] == 'X') or \n",
" (board[7] == 'X' and board[8] == 'X')):\n",
" board[6] = 'O'\n",
" elif ((board[3] == 'X' and board[6] == 'X') or \n",
" (board[1] == 'X' and board[2] == 'X')):\n",
" board[0] = 'O'\n",
" elif ((board[2] == 'X' and board[5] == 'X') or \n",
" (board[6] == 'X' and board[7] == 'X')):\n",
" board[8] = 'O'\n",
" \n",
" elif board[0] == 'X' and board[2] == 'X':\n",
" board[1] = 'O'\n",
" elif board[0] == 'X' and board[6] == 'X':\n",
" board[3] = 'O'\n",
" elif board[2] == 'X' and board[8] == 'X':\n",
" board[5] = 'O'\n",
" elif board[6] == 'X' and board[8] == 'X':\n",
" board[7] = 'O'\n",
" \n",
" elif board[3] == ' ' and ((board[6] == 'X' and board[2] == 'X') or (board[0] == 'X' and board[8] == 'X')):\n",
" board[3] = 'O'\n",
" else:\n",
" if (board[0] == ' ') and ((board[1] == 'X' and board[3] == 'X') or (board[3] == 'X' and board[2] == 'X') or (board[1] == 'X' and board[6] == 'X')):\n",
" board[0] = 'O'\n",
" elif (board[2] == ' ') and ((board[1] == 'X' and board[5] == 'X') or (board[1] == 'X' and board[8] == 'X') or (board[0] == 'X' and board[5] == 'X')):\n",
" board[2] = 'O'\n",
" elif (board[6] == ' ') and ((board[3] == 'X' and board[7] == 'X') or (board[3] == 'X' and board[8] == 'X') or (board[0] == 'X' and board[7] == 'X')):\n",
" board[6] = 'O'\n",
" elif (board[8] == ' ') and ((board[7] == 'X' and board[5] == 'X') or (board[7] == 'X' and board[2] == 'X') or (board[6] == 'X' and board[5] == 'X')):\n",
" board[8] = 'O'\n",
" else:\n",
" board[randcornerpos(board)] = 'O'\n",
" \n",
" else:\n",
" if board[1] == 'X':\n",
" board[7] = 'O'\n",
" elif board[2] == 'X':\n",
" board[6] = 'O'\n",
" elif board[3] == 'X':\n",
" board[5] = 'O'\n",
" elif board[5] == 'X':\n",
" board[3] = 'O'\n",
" elif board[6] == 'X':\n",
" board[2] = 'O'\n",
" elif board[7] == 'X':\n",
" board[1] = 'O'\n",
" else:\n",
" board[randcornerpos(board)] = 'O'\n",
" \n",
" elif move == 5:\n",
" if firstMove == 4:\n",
" if (board[8] == 'O' and board[0] == ' '):\n",
" board[0] = 'O'\n",
" elif (board[7] == 'O' and board[1] == ' '):\n",
" board[1] = 'O'\n",
" elif (board[6] == 'O' and board[2] == ' '):\n",
" board[2] = 'O'\n",
" elif (board[5] == 'O' and board[3] == ' '):\n",
" board[3] = 'O'\n",
" elif (board[3] == 'O' and board[5] == ' '):\n",
" board[5] = 'O'\n",
" elif (board[2] == 'O' and board[6] == ' '):\n",
" board[6] = 'O'\n",
" elif (board[1] == 'O' and board[7] == ' '):\n",
" board[7] = 'O'\n",
" elif (board[0] == 'O' and board[8] == ' '):\n",
" board[8] = 'O'\n",
" elif ((board[0] == 'X' and board[1] == 'X') or \n",
" (board[5] == 'X' and board[8] == 'X')) and board[2] == ' ':\n",
" board[2] = 'O'\n",
" elif ((board[0] == 'X' and board[3] == 'X') or \n",
" (board[7] == 'X' and board[8] == 'X')) and board[6] == ' ':\n",
" board[6] = 'O'\n",
" elif ((board[3] == 'X' and board[6] == 'X') or \n",
" (board[1] == 'X' and board[2] == 'X')) and board[0] == ' ':\n",
" board[0] = 'O'\n",
" elif ((board[2] == 'X' and board[5] == 'X') or \n",
" (board[6] == 'X' and board[7] == 'X')) and board[8] == ' ':\n",
" board[8] = 'O'\n",
" \n",
" elif board[0] == 'X' and board[2] == 'X' and board[1] == ' ':\n",
" board[1] = 'O'\n",
" elif board[0] == 'X' and board[6] == 'X' and board[3] == ' ':\n",
" board[3] = 'O'\n",
" elif board[2] == 'X' and board[8] == 'X' and board[5] == ' ':\n",
" board[5] = 'O'\n",
" elif board[6] == 'X' and board[8] == 'X' and board[7] == ' ':\n",
" board[7] = 'O'\n",
" \n",
" elif board[3] == ' ':\n",
" board[3] = 'O'\n",
" else:\n",
" board[randpos(board)] = 'O'\n",
" else:\n",
" if ((board[1] == 'O' and board[2] == 'O') or (board[3] == 'O' and board[6] == 'O')) and board[0] == ' ':\n",
" board[0] = 'O'\n",
" elif (board[0] == 'O' and board[2] == 'O' and board[1] == ' '):\n",
" board[1] = 'O'\n",
" elif ((board[0] == 'O' and board[1] == 'O') or (board[5] == 'O' and board[8] == 'O')) and board[2] == ' ':\n",
" board[2] = 'O'\n",
" elif (board[0] == 'O' and board[6] == 'O' and board[3] == ' '):\n",
" board[3] = 'O'\n",
" elif (board[2] == 'O' and board[8] == 'O' and board[5] == ' '):\n",
" board[5] = 'O'\n",
" elif ((board[0] == 'O' and board[3] == 'O') or (board[7] == 'O' and board[8] == 'O')) and board[6] == ' ':\n",
" board[6] = 'O'\n",
" elif (board[6] == 'O' and board[8] == 'O' and board[7] == ' '):\n",
" board[7] = 'O'\n",
" elif ((board[2] == 'O' and board[5] == 'O') or (board[6] == 'O' and board[7] == 'O')) and board[8] == ' ':\n",
" board[8] = 'O'\n",
" elif board[1] == 'X' and board[7] == ' ':\n",
" board[7] = 'O'\n",
" elif board[2] == 'X' and board[6] == ' ':\n",
" board[6] = 'O'\n",
" elif board[3] == 'X' and board[5] == ' ':\n",
" board[5] = 'O'\n",
" elif board[5] == 'X' and board[3] == ' ':\n",
" board[3] = 'O'\n",
" elif board[6] == 'X' and board[2] == ' ':\n",
" board[2] = 'O'\n",
" elif board[7] == 'X' and board[1] == ' ':\n",
" board[1] = 'O'\n",
" elif board[3] == ' ':\n",
" board[3] = 'O'\n",
" else:\n",
" board[randcornerpos(board)] = 'O'\n",
" elif move == 7:\n",
" if firstMove == 4:\n",
" if ((board[0] == 'X' and board[1] == 'X') or \n",
" (board[5] == 'X' and board[7] == 'X')) and board[2] == ' ':\n",
" board[2] = 'O'\n",
" elif ((board[0] == 'X' and board[3] == 'X') or \n",
" (board[7] == 'X' and board[8] == 'X')) and board[6] == ' ':\n",
" board[6] = 'O'\n",
" elif ((board[3] == 'X' and board[6] == 'X') or \n",
" (board[1] == 'X' and board[2] == 'X')) and board[0] == ' ':\n",
" board[0] = 'O'\n",
" elif ((board[2] == 'X' and board[5] == 'X') or \n",
" (board[6] == 'X' and board[7] == 'X')) and board[8] == ' ':\n",
" board[8] = 'O'\n",
" \n",
" elif board[0] == 'X' and board[2] == 'X' and board[1] == ' ':\n",
" board[1] = 'O'\n",
" elif board[0] == 'X' and board[6] == 'X' and board[3] == ' ':\n",
" board[3] = 'O'\n",
" elif board[2] == 'X' and board[8] == 'X' and board[5] == ' ':\n",
" board[5] = 'O'\n",
" elif board[6] == 'X' and board[8] == 'X' and board[7] == ' ':\n",
" board[7] = 'O'\n",
" \n",
" elif board[3] == ' ':\n",
" board[3] = 'O'\n",
" else:\n",
" board[randpos(board)] = 'O'\n",
" else:\n",
" if board[1] == 'X' and board[7] != 'O':\n",
" board[7] = 'O'\n",
" elif board[2] == 'X' and board[6] != 'O':\n",
" board[6] = 'O'\n",
" elif board[3] == 'X' and board[5] != 'O':\n",
" board[5] = 'O'\n",
" elif board[5] == 'X' and board[3] != 'O':\n",
" board[3] = 'O'\n",
" elif board[6] == 'X' and board[2] != 'O':\n",
" board[2] = 'O'\n",
" elif board[7] == 'X' and board[1] != 'O':\n",
" board[1] = 'O'\n",
" elif board[3] == ' ':\n",
" board[3] = 'O'\n",
" else:\n",
" board[randpos(board)] = 'O'"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# B: Tra Lam Thanh Vo, Jessica Nicholson"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def player1_b(move, board):\n",
" if move==0:\n",
" board[4]= 'X'\n",
" elif move==2:\n",
" board[0]= 'X'\n",
" elif move==4:\n",
" board[5]= 'X'\n",
" else:\n",
" board[randpos(board)] = 'X'"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def player2_b(move, board):\n",
" if move==1:\n",
" board[1]='O'\n",
" elif move==3:\n",
" board[8]='O'\n",
" elif move ==5:\n",
" board[3]= 'O'\n",
" else:\n",
" board[randpos(board)] = 'O'"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# C: Jamel Addahoumi"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def player1_c(move, board):\n",
" mark = 'X'\n",
" oppMark = 'O'\n",
" \n",
" possibleWinsets = [(0,1,2), (3,4,5), (6,7,8), (0,3,6), \n",
" (1,4,7), (2,5,8), (0,4,8), (2,4,6)]\n",
" selectedCases = []\n",
" markPriority = [0,0,0,0,\n",
" 0,0,0,0]\n",
" slot = 0\n",
" caseIdx = 0\n",
" finalMarkIdx = 0\n",
" \n",
" slotArr = [0,1,2,\n",
" 3,4,5,\n",
" 6,7,8]\n",
" \n",
" slotPrior = [1,0,1,\n",
" 0,0,0,\n",
" 1,0,1]\n",
" chosenIdx = 0\n",
" #looking for possible wins\n",
" for mark in board.board: #marks in board\n",
" if(True):\n",
" caseIdx = 0\n",
" for case in possibleWinsets: #case tuples in winsets\n",
" for winMark in case: #markloc in case\n",
" \n",
" if mark is 'O':\n",
" if(winMark is slot):\n",
" print(caseIdx)\n",
" markPriority[caseIdx] -= 10\n",
" \n",
" if mark is 'X':\n",
" if(winMark is slot):\n",
" print(caseIdx)\n",
" markPriority[caseIdx] += 1\n",
" caseIdx+=1\n",
" slot+=1 \n",
" \n",
" \n",
" currChooseVal = -500\n",
" selectedCase = ()\n",
" caseIdx = 0\n",
" direWarning = [0,0,0,0,\n",
" 0,0,0,0]\n",
" direIdx = 0\n",
" defense = False\n",
" \n",
" for val in markPriority:\n",
" if(val is 2):\n",
" for markIdx in possibleWinsets[caseIdx]:\n",
" if(board.board[markIdx] is not 'X'):\n",
" chosenIdx = markIdx\n",
" board[chosenIdx] = 'X'\n",
" #board[markIdx] = 'X'\n",
" return\n",
" caseIdx+=1\n",
" \n",
" \n",
" \n",
" for case in markPriority:\n",
"# print('first ZING')\n",
" if(case == -20):\n",
"# print('ZING')\n",
" for diremark in possibleWinsets[direIdx]:\n",
" if(board.board[diremark] == ' '):\n",
" \n",
" board[diremark] = 'X'\n",
" #board[direIdx] = 'X'\n",
" return\n",
" \n",
" direIdx+=1 \n",
" \n",
" idx = 0 \n",
" #slotprior\n",
" for mark in board:\n",
" if(idx is 0):\n",
" if(mark is 'O'):\n",
" slotPrior[0] -= 10\n",
" else:\n",
" slotPrior[0] += 1\n",
" if(idx is 2):\n",
" if mark is 'O':\n",
" slotPrior[2] -= 10\n",
" else:\n",
" slotPrior[2] += 1 \n",
" if(idx is 6):\n",
" if mark is 'O':\n",
" slotPrior[6] -= 10\n",
" else:\n",
" slotPrior[6] += 1 \n",
" if(idx is 8):\n",
" if mark is 'O':\n",
" slotPrior[8] -= 10\n",
" else:\n",
" slotPrior[8] += 1 \n",
" idx += 1\n",
" \n",
" idxVal = 0\n",
" largestIdxVal = 0\n",
" bidx = 0\n",
" for mark in board:\n",
" if(board[bidx] == 'X' or board[bidx] == 'O'):\n",
" slotPrior[bidx] -= 10\n",
" bidx += 1 \n",
" for i in slotPrior:\n",
" if(i > largestIdxVal):\n",
" largestIdxVal = i\n",
" chosenIdx = idxVal\n",
" idxVal += 1 \n",
" \n",
" \n",
" \n",
" board[chosenIdx] = 'X'\n",
" \n",
" return"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def player2_c(move, board):\n",
" if(move == 1 and board[4] != 'X'):\n",
" board[4] = 'O'\n",
" return\n",
" \n",
" mark = 'O'\n",
" oppMark = 'X'\n",
" \n",
" possibleWinsets = [(0,1,2), (3,4,5), (6,7,8), (0,3,6), \n",
" (1,4,7), (2,5,8), (0,4,8), (2,4,6)]\n",
" selectedCases = []\n",
" markPriority = [0,0,0,0,\n",
" 0,0,0,0]\n",
" slot = 0\n",
" caseIdx = 0\n",
" finalMarkIdx = 0\n",
" \n",
" slotArr = [0,1,2,\n",
" 3,4,5,\n",
" 6,7,8]\n",
" \n",
" slotPrior = [1,0,1,\n",
" 0,0,0,\n",
" 1,0,1]\n",
" chosenIdx = 0\n",
" #looking for possible wins\n",
" for mark in board.board: #marks in board\n",
" if(True):\n",
" caseIdx = 0\n",
" for case in possibleWinsets: #case tuples in winsets\n",
" for winMark in case: #markloc in case\n",
" \n",
" if mark is 'X':\n",
" if(winMark == slot):\n",
" print(caseIdx)\n",
" markPriority[caseIdx] -= 10\n",
" \n",
" if mark is 'O':\n",
" if(winMark == slot):\n",
" print(caseIdx)\n",
" markPriority[caseIdx] += 1\n",
" caseIdx+=1\n",
" slot+=1 \n",
" \n",
" \n",
" currChooseVal = -500\n",
" selectedCase = ()\n",
" caseIdx = 0\n",
" direWarning = [0,0,0,0,\n",
" 0,0,0,0]\n",
" direIdx = 0\n",
" defense = False\n",
" print(markPriority)\n",
" for case in markPriority:\n",
" print('first ZING')\n",
" if(case == -20):\n",
" print('ZING')\n",
" for diremark in possibleWinsets[direIdx]:\n",
" if(board.board[diremark] == ' '):\n",
" \n",
" board[diremark] = 'O'\n",
" #board[direIdx] = 'X'\n",
" return\n",
" \n",
" direIdx+=1\n",
" \n",
" for val in markPriority:\n",
" if(val is 2):\n",
" for markIdx in possibleWinsets[caseIdx]:\n",
" if(board.board[markIdx] == ' '):\n",
" chosenIdx = markIdx\n",
" board[chosenIdx] = 'O'\n",
" #board[markIdx] = 'X'\n",
" return\n",
" caseIdx+=1\n",
" \n",
" \n",
" \n",
" for case in markPriority:\n",
" if(case is -20):\n",
" for mark in possibleWinsets[direIdx]:\n",
" if(board.board[mark] == ' '):\n",
" chosenIdx = mark\n",
" board[chosenIdx] = 'O'\n",
" #board[direIdx] = 'X'\n",
" return\n",
" \n",
" direIdx+=1 \n",
" \n",
" idx = 0 \n",
" #slotprior\n",
" for mark in board:\n",
" if(idx is 0):\n",
" if(mark is 'X'):\n",
" slotPrior[0] -= 10\n",
" else:\n",
" slotPrior[0] += 1\n",
" if(idx is 2):\n",
" if mark is 'X':\n",
" slotPrior[2] -= 10\n",
" else:\n",
" slotPrior[2] += 1 \n",
" if(idx is 6):\n",
" if mark is 'X':\n",
" slotPrior[6] -= 10\n",
" else:\n",
" slotPrior[6] += 1 \n",
" if(idx is 8):\n",
" if mark is 'X':\n",
" slotPrior[8] -= 10\n",
" else:\n",
" slotPrior[8] += 1 \n",
" idx += 1\n",
" \n",
" idxVal = 0\n",
" largestIdxVal = 0\n",
" bidx = 0\n",
" for mark in board:\n",
" if(board[bidx] == 'O' or board[bidx] == 'X'):\n",
" slotPrior[bidx] -= 10\n",
" bidx += 1 \n",
" for i in slotPrior:\n",
" if(i > largestIdxVal):\n",
" largestIdxVal = i\n",
" chosenIdx = idxVal\n",
" idxVal += 1 \n",
" \n",
" \n",
" \n",
" board[chosenIdx] = 'O'\n",
" \n",
" return"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# D: Linda Chen"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def player1_d(move, board):\n",
" if move == 0:\n",
" board[4] = 'X'\n",
" elif move == 2 and board[0] == 'O':\n",
" board[2] = 'X'\n",
" elif move == 2 and board[1] == ' ':\n",
" board[1] = 'X'\n",
" elif move == 2 and board[3] == ' ':\n",
" board[3] = 'X'\n",
" elif move == 2 and board[7] == ' ':\n",
" board[7] = 'X'\n",
" elif move == 2 and board[1] == ' ':\n",
" board[1] = 'X'\n",
" \n",
" elif move == 4 and board[4] == 'X' and board[1] == 'X'and board[7] == ' ':\n",
" board[7] = 'X'\n",
" elif move == 4 and board[4] == 'X' and board[3] == 'X'and board[5] == ' ':\n",
" board[5] = 'X'\n",
" elif move == 4 and board[4] == 'X' and board[7] == 'X'and board[1] == ' ':\n",
" board[1] = 'X'\n",
" elif move == 4 and board[4] == 'X' and board[5] == 'X'and board[3] == ' ':\n",
" board[3] = 'X'\n",
" elif move == 4 and board[4] == 'X' and board[0] == 'X'and board[8] == ' ':\n",
" board[8] = 'X'\n",
" elif move == 4 and board[4] == 'X' and board[8] == 'X'and board[0] == ' ':\n",
" board[0] = 'X'\n",
" elif move == 4 and board[4] == 'X' and board[2] == 'X'and board[6] == ' ':\n",
" board[6] = 'X'\n",
" elif move == 4 and board[4] == 'X' and board[6] == 'X'and board[2] == ' ':\n",
" board[2] = 'X'\n",
" \n",
" elif move == 4 and board[0] == 'O' and board[1] == 'O'and board[2] != 'X':\n",
" board[2] = 'X'\n",
" elif move == 4 and board[1] == 'O' and board[2] == 'O'and board[0] != 'X':\n",
" board[0] = 'X'\n",
" elif move == 4 and board[0] == 'O' and board[2] == 'O'and board[1] != 'X':\n",
" board[1] = 'X'\n",
" elif move == 4 and board[3] == 'O' and board[4] == 'O'and board[5] != 'X':\n",
" board[5] = 'X'\n",
" elif move == 4 and board[4] == 'O' and board[5] == 'O'and board[3] != 'X':\n",
" board[3] = 'X'\n",
" elif move == 4 and board[3] == 'O' and board[5] == 'O'and board[4] != 'X':\n",
" board[4] = 'X'\n",
" elif move == 4 and board[6] == 'O' and board[7] == 'O'and board[8] != 'X':\n",
" board[8] = 'X'\n",
" elif move == 4 and board[7] == 'O' and board[8] == 'O'and board[6] != 'X':\n",
" board[6] = 'X'\n",
" elif move == 4 and board[6] == 'O' and board[8] == 'O'and board[7] != 'X':\n",
" board[7] = 'X'\n",
" elif move == 4 and board[0] == 'O' and board[3] == 'O'and board[6] != 'X':\n",
" board[6] = 'X'\n",
" elif move == 4 and board[3] == 'O' and board[6] == 'O'and board[0] != 'X':\n",
" board[0] = 'X'\n",
" elif move == 4 and board[0] == 'O' and board[6] == 'O'and board[3] != 'X':\n",
" board[3] = 'X'\n",
" elif move == 4 and board[1] == 'O' and board[4] == 'O'and board[7] != 'X':\n",
" board[7] = 'X'\n",
" elif move == 4 and board[1] == 'O' and board[7] == 'O'and board[4] != 'X':\n",
" board[4] = 'X'\n",
" elif move == 4 and board[4] == 'O' and board[7] == 'O'and board[1] != 'X':\n",
" board[1] = 'X'\n",
" elif move == 4 and board[2] == 'O' and board[5] == 'O'and board[8] != 'X':\n",
" board[8] = 'X'\n",
" elif move == 4 and board[5] == 'O' and board[8] == 'O'and board[2] != 'X':\n",
" board[2] = 'X' \n",
" elif move == 4 and board[2] == 'O' and board[8] == 'O'and board[5] != 'X':\n",
" board[5] = 'X' \n",
" elif move == 4 and board[0] == 'O' and board[4] == 'O'and board[8] != 'X':\n",
" board[8] = 'X' \n",
" elif move == 4 and board[0] == 'O' and board[8] == 'O'and board[4] != 'X':\n",
" board[4] = 'X' \n",
" elif move == 4 and board[4] == 'O' and board[8] == 'O'and board[0] != 'X':\n",
" board[0] = 'X' \n",
" elif move == 4 and board[2] == 'O' and board[4] == 'O'and board[6] != 'X':\n",
" board[6] = 'X' \n",
" elif move == 4 and board[2] == 'O' and board[6] == 'O'and board[4] != 'X':\n",
" board[4] = 'X' \n",
" elif move == 4 and board[4] == 'O' and board[6] == 'O'and board[2] != 'X':\n",
" board[2] = 'X'\n",
" \n",
" \n",
" elif move == 4 and board[0] == ' ':\n",
" board[0] = 'X'\n",
" elif move == 4 and board[1] == ' ':\n",
" board[1] = 'X'\n",
" elif move == 4 and board[2] == ' ':\n",
" board[2] = 'X'\n",
" elif move == 4 and board[3] == ' ':\n",
" board[3] = 'X'\n",
" elif move == 4 and board[4] == ' ':\n",
" board[4] = 'X'\n",
" elif move == 4 and board[5] == ' ':\n",
" board[5] = 'X'\n",
" elif move == 4 and board[6] == ' ':\n",
" board[6] = 'X'\n",
" elif move == 4 and board[7] == ' ':\n",
" board[7] = 'X'\n",
" elif move == 4 and board[8] == ' ':\n",
" board[8] = 'X'\n",
" \n",
" \n",
" elif move == 4 and board[2]=='X' and board[6]=='O':\n",
" board[3]='X'\n",
" elif move == 4 and board[5]=='X' and board[3]=='O':\n",
" board[6]='X'\n",
" elif move == 4 and board[7]=='X' and board[1]=='O':\n",
" board[2]='X'\n",
" elif move == 4 and board[8]=='X' and board[6]=='O':\n",
" board[3]='X'\n",
" elif move == 4 and board[8]=='X' and board[2]=='O':\n",
" board[1]='X'\n",
" \n",
" elif move == 6 and board[4] == 'X' and board[1] == 'X'and board[7] == ' ':\n",
" board[7] = 'X'\n",
" elif move == 6 and board[4] == 'X' and board[3] == 'X'and board[5] == ' ':\n",
" board[5] = 'X'\n",
" elif move == 6 and board[4] == 'X' and board[7] == 'X'and board[1] == ' ':\n",
" board[1] = 'X'\n",
" elif move == 6 and board[4] == 'X' and board[5] == 'X'and board[3] == ' ':\n",
" board[3] = 'X'\n",
" elif move == 6 and board[4] == 'X' and board[0] == 'X'and board[8] == ' ':\n",
" board[8] = 'X'\n",
" elif move == 6 and board[4] == 'X' and board[8] == 'X'and board[0] == ' ':\n",
" board[0] = 'X'\n",
" elif move == 6 and board[4] == 'X' and board[2] == 'X'and board[6] == ' ':\n",
" board[6] = 'X'\n",
" elif move == 6 and board[4] == 'X' and board[6] == 'X'and board[2] == ' ':\n",
" board[2] = 'X'\n",
" \n",
" elif move == 6 and board[0] == 'O' and board[1] == 'O'and board[2] != 'X':\n",
" board[2] = 'X'\n",
" elif move == 6 and board[1] == 'O' and board[2] == 'O'and board[0] != 'X':\n",
" board[0] = 'X'\n",
" elif move == 6 and board[0] == 'O' and board[2] == 'O'and board[1] != 'X':\n",
" board[1] = 'X'\n",
" elif move == 6 and board[3] == 'O' and board[4] == 'O'and board[5] != 'X':\n",
" board[5] = 'X'\n",
" elif move == 6 and board[4] == 'O' and board[5] == 'O'and board[3] != 'X':\n",
" board[3] = 'X'\n",
" elif move == 6 and board[3] == 'O' and board[5] == 'O'and board[4] != 'X':\n",
" board[4] = 'X'\n",
" elif move == 6 and board[6] == 'O' and board[7] == 'O'and board[8] != 'X':\n",
" board[8] = 'X'\n",
" elif move == 6 and board[7] == 'O' and board[8] == 'O'and board[6] != 'X':\n",
" board[6] = 'X'\n",
" elif move == 6 and board[6] == 'O' and board[8] == 'O'and board[7] != 'X':\n",
" board[7] = 'X'\n",
" elif move == 6 and board[0] == 'O' and board[3] == 'O'and board[6] != 'X':\n",
" board[6] = 'X'\n",
" elif move == 6 and board[3] == 'O' and board[6] == 'O'and board[0] != 'X':\n",
" board[0] = 'X'\n",
" elif move == 6 and board[0] == 'O' and board[6] == 'O'and board[3] != 'X':\n",
" board[3] = 'X'\n",
" elif move == 6 and board[1] == 'O' and board[4] == 'O'and board[7] != 'X':\n",
" board[7] = 'X'\n",
" elif move == 6 and board[1] == 'O' and board[7] == 'O'and board[4] != 'X':\n",
" board[4] = 'X'\n",
" elif move == 6 and board[4] == 'O' and board[7] == 'O'and board[1] != 'X':\n",
" board[1] = 'X'\n",
" elif move == 6 and board[2] == 'O' and board[5] == 'O'and board[8] != 'X':\n",
" board[8] = 'X'\n",
" elif move == 6 and board[5] == 'O' and board[8] == 'O'and board[2] != 'X':\n",
" board[2] = 'X' \n",
" elif move == 6 and board[2] == 'O' and board[8] == 'O'and board[5] != 'X':\n",
" board[5] = 'X' \n",
" elif move == 6 and board[0] == 'O' and board[4] == 'O'and board[8] != 'X':\n",
" board[8] = 'X' \n",
" elif move == 6 and board[0] == 'O' and board[8] == 'O'and board[4] != 'X':\n",
" board[4] = 'X' \n",
" elif move == 6 and board[4] == 'O' and board[8] == 'O'and board[0] != 'X':\n",
" board[0] = 'X' \n",
" elif move == 6 and board[2] == 'O' and board[4] == 'O'and board[6] != 'X':\n",
" board[6] = 'X' \n",
" elif move == 6 and board[2] == 'O' and board[6] == 'O'and board[4] != 'X':\n",
" board[4] = 'X' \n",
" elif move == 6 and board[4] == 'O' and board[6] == 'O'and board[2] != 'X':\n",
" board[2] = 'X'\n",
" \n",
" \n",
" elif move == 6 and board[5] ==' ':\n",
" board[5]\n",
" elif move == 6 and board[1]==' ':\n",
" board[1]='X'\n",
" elif move == 6 and board[7]==' ':\n",
" board[7]='X'\n",
" elif move == 6 and board[0] == ' ':\n",
" board[0] = 'X'\n",
" elif move == 6 and board[1] == ' ':\n",
" board[1] = 'X'\n",
" elif move == 6 and board[2] == ' ':\n",
" board[2] = 'X'\n",
" elif move == 6 and board[3] == ' ':\n",
" board[3] = 'X'\n",
" elif move == 6 and board[4] == ' ':\n",
" board[4] = 'X'\n",
" elif move == 6 and board[5] == ' ':\n",
" board[5] = 'X'\n",
" elif move == 6 and board[6] == ' ':\n",
" board[6] = 'X'\n",
" elif move == 6 and board[7] == ' ':\n",
" board[7] = 'X'\n",
" elif move == 6 and board[8] == ' ':\n",
" board[8] = 'X'\n",
" \n",
" \n",
" elif move == 8 and board[0] == ' ':\n",
" board[0] = 'X'\n",
" elif move == 8 and board[1] == ' ':\n",
" board[1] = 'X'\n",
" elif move == 8 and board[2] == ' ':\n",
" board[2] = 'X'\n",
" elif move == 8 and board[3] == ' ':\n",
" board[3] = 'X'\n",
" elif move == 8 and board[4] == ' ':\n",
" board[4] = 'X'\n",
" elif move == 8 and board[5] == ' ':\n",
" board[5] = 'X'\n",
" elif move == 8 and board[6] == ' ':\n",
" board[6] = 'X'\n",
" elif move == 8 and board[7] == ' ':\n",
" board[7] = 'X'\n",
" elif move == 8 and board[8] == ' ':\n",
" board[8] = 'X'"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def player2_d(move, board):\n",
" if move == 1 and board[4] != 'X':\n",
" board[4] = 'O'\n",
" elif move == 1 and board[4] == 'X':\n",
" board[0] = 'O'\n",
" \n",
" elif move == 3 and board[0] == 'X' and board[1] == 'X'and board[2] != 'O':\n",
" board[2] = 'O'\n",
" elif move == 3 and board[1] == 'X' and board[2] == 'X'and board[0] != 'O':\n",
" board[0] = 'O'\n",
" elif move == 3 and board[0] == 'X' and board[2] == 'X'and board[1] != 'O':\n",
" board[1] = 'O'\n",
" elif move == 3 and board[3] == 'X' and board[4] == 'X'and board[5] != 'O':\n",
" board[5] = 'O'\n",
" elif move == 3 and board[4] == 'X' and board[5] == 'X'and board[3] != 'O':\n",
" board[3] = 'O'\n",
" elif move == 3 and board[3] == 'X' and board[5] == 'X'and board[4] != 'O':\n",
" board[4] = 'O'\n",
" elif move == 3 and board[6] == 'X' and board[7] == 'X'and board[8] != 'O':\n",
" board[8] = 'O'\n",
" elif move == 3 and board[7] == 'X' and board[8] == 'X'and board[6] != 'O':\n",
" board[6] = 'O'\n",
" elif move == 3 and board[6] == 'X' and board[8] == 'X'and board[7] != 'O':\n",
" board[7] = 'O'\n",
" elif move == 3 and board[0] == 'X' and board[3] == 'X'and board[6] != 'O':\n",
" board[6] = 'O'\n",
" elif move == 3 and board[3] == 'X' and board[6] == 'X'and board[0] != 'O':\n",
" board[0] = 'O'\n",
" elif move == 3 and board[0] == 'X' and board[6] == 'X'and board[3] != 'O':\n",
" board[3] = 'O'\n",
" elif move == 3 and board[1] == 'X' and board[4] == 'X'and board[7] != 'O':\n",
" board[7] = 'O'\n",
" elif move == 3 and board[1] == 'X' and board[7] == 'X'and board[4] != 'O':\n",
" board[4] = 'O'\n",
" elif move == 3 and board[4] == 'X' and board[7] == 'X'and board[1] != 'O':\n",
" board[1] = 'O'\n",
" elif move == 3 and board[2] == 'X' and board[5] == 'X'and board[8] != 'O':\n",
" board[8] = 'O'\n",
" elif move == 3 and board[5] == 'X' and board[8] == 'X'and board[2] != 'O':\n",
" board[2] = 'O' \n",
" elif move == 3 and board[2] == 'X' and board[8] == 'X'and board[5] != 'O':\n",
" board[5] = 'O' \n",
" elif move == 3 and board[0] == 'X' and board[4] == 'X'and board[8] != 'O':\n",
" board[8] = 'O' \n",
" elif move == 3 and board[0] == 'X' and board[8] == 'X'and board[4] != 'O':\n",
" board[4] = 'O' \n",
" elif move == 3 and board[4] == 'X' and board[8] == 'X'and board[0] != 'O':\n",
" board[0] = 'O' \n",
" elif move == 3 and board[2] == 'X' and board[4] == 'X'and board[6] != 'O':\n",
" board[6] = 'O' \n",
" elif move == 3 and board[2] == 'X' and board[6] == 'X'and board[4] != 'O':\n",
" board[4] = 'O' \n",
" elif move == 3 and board[4] == 'X' and board[6] == 'X'and board[2] != 'O':\n",
" board[2] = 'O'\n",
" \n",
" \n",
" elif move == 3 and board[8] == ' ':\n",
" board[8] = 'O'\n",
" elif move == 3 and board[1] == ' ':\n",
" board[1] = 'O'\n",
" elif move == 3 and board[3] == ' ':\n",
" board[3] = 'O'\n",
" \n",
" elif move == 5 and board[4] == 'O' and board[1] == 'O'and board[7] == ' ':\n",
" board[7] = 'O'\n",
" elif move == 5 and board[4] == 'O' and board[3] == 'O'and board[5] == ' ':\n",
" board[5] = 'O'\n",
" elif move == 5 and board[4] == 'O' and board[8] == 'O'and board[0] == ' ':\n",
" board[0] = 'O'\n",
" elif move == 5 and board[4] == 'O' and board[8] == 'O'and board[2] == ' ':\n",
" board[2] = 'O'\n",
" elif move == 5 and board[4] == 'O' and board[8] == 'O'and board[6] == ' ':\n",
" board[6] = 'O'\n",
" elif move == 5 and board[0] == 'O' and board[1] == 'O'and board[2] == ' ':\n",
" board[2] = 'O'\n",
" elif move == 5 and board[0] == 'O' and board[3] == 'O'and board[6] == ' ':\n",
" board[6] = 'O'\n",
" elif move == 5 and board[0] == 'O' and board[8] == 'O'and board[4] == ' ':\n",
" board[4] = 'O'\n",
" elif move == 5 and board[0] == 'O' and board[8] == 'O'and board[2] == ' ':\n",
" board[2] = 'O'\n",
" elif move == 5 and board[0] == 'O' and board[8] == 'O'and board[6] == ' ':\n",
" board[6] = 'O'\n",
" \n",
" elif move == 5 and board[0] == 'X' and board[1] == 'X'and board[2] != 'O':\n",
" board[2] = 'O'\n",
" elif move == 5 and board[1] == 'X' and board[2] == 'X'and board[0] != 'O':\n",
" board[0] = 'O'\n",
" elif move == 5 and board[0] == 'X' and board[2] == 'X'and board[1] != 'O':\n",
" board[1] = 'O'\n",
" elif move == 5 and board[3] == 'X' and board[4] == 'X'and board[5] != 'O':\n",
" board[5] = 'O'\n",
" elif move == 5 and board[4] == 'X' and board[5] == 'X'and board[3] != 'O':\n",
" board[3] = 'O'\n",
" elif move == 5 and board[3] == 'X' and board[5] == 'X'and board[4] != 'O':\n",
" board[4] = 'O'\n",
" elif move == 5 and board[6] == 'X' and board[7] == 'X'and board[8] != 'O':\n",
" board[8] = 'O'\n",
" elif move == 5 and board[7] == 'X' and board[8] == 'X'and board[6] != 'O':\n",
" board[6] = 'O'\n",
" elif move == 5 and board[6] == 'X' and board[8] == 'X'and board[7] != 'O':\n",
" board[7] = 'O'\n",
" elif move == 5 and board[0] == 'X' and board[3] == 'X'and board[6] != 'O':\n",
" board[6] = 'O'\n",
" elif move == 5 and board[3] == 'X' and board[6] == 'X'and board[0] != 'O':\n",
" board[0] = 'O'\n",
" elif move == 5 and board[0] == 'X' and board[6] == 'X'and board[3] != 'O':\n",
" board[3] = 'O'\n",
" elif move == 5 and board[1] == 'X' and board[4] == 'X'and board[7] != 'O':\n",
" board[7] = 'O'\n",
" elif move == 5 and board[1] == 'X' and board[7] == 'X'and board[4] != 'O':\n",
" board[4] = 'O'\n",
" elif move == 5 and board[4] == 'X' and board[7] == 'X'and board[1] != 'O':\n",
" board[1] = 'O'\n",
" elif move == 5 and board[2] == 'X' and board[5] == 'X'and board[8] != 'O':\n",
" board[8] = 'O'\n",
" elif move == 5 and board[5] == 'X' and board[8] == 'X'and board[2] != 'O':\n",
" board[2] = 'O' \n",
" elif move == 5 and board[2] == 'X' and board[8] == 'X'and board[5] != 'O':\n",
" board[5] = 'O' \n",
" elif move == 5 and board[0] == 'X' and board[4] == 'X'and board[8] != 'O':\n",
" board[8] = 'O' \n",
" elif move == 5 and board[0] == 'X' and board[8] == 'X'and board[4] != 'O':\n",
" board[4] = 'O' \n",
" elif move == 5 and board[4] == 'X' and board[8] == 'X'and board[0] != 'O':\n",
" board[0] = 'O' \n",
" elif move == 5 and board[2] == 'X' and board[4] == 'X'and board[6] != 'O':\n",
" board[6] = 'O' \n",
" elif move == 5 and board[2] == 'X' and board[6] == 'X'and board[4] != 'O':\n",
" board[4] = 'O' \n",
" elif move == 5 and board[4] == 'X' and board[6] == 'X'and board[2] != 'O':\n",
" board[2] = 'O'\n",
" \n",
" \n",
" elif move == 5 and board[1] == ' ':\n",
" board[1] = 'O'\n",
" elif move == 5 and board[2] == ' ':\n",
" board[2] = 'O'\n",
" elif move == 5 and board[3] == ' ':\n",
" board[3] = 'O'\n",
" elif move == 5 and board[4] == ' ':\n",
" board[4] = 'O'\n",
" elif move == 5 and board[5] == ' ':\n",
" board[5] = 'O'\n",
" elif move == 5 and board[6] == ' ':\n",
" board[6] = 'O'\n",
" elif move == 5 and board[7] == ' ':\n",
" board[7] = 'O'\n",
" elif move == 5 and board[8] == ' ':\n",
" board[8] = 'O'\n",
" \n",
" elif move == 7 and board[4] == 'O' and board[1] == 'O'and board[7] == ' ':\n",
" board[7] = 'O'\n",
" elif move == 7 and board[4] == 'O' and board[3] == 'O'and board[5] == ' ':\n",
" board[5] = 'O'\n",
" elif move == 7 and board[4] == 'O' and board[8] == 'O'and board[0] == ' ':\n",
" board[0] = 'O'\n",
" elif move == 7 and board[4] == 'O' and board[8] == 'O'and board[2] == ' ':\n",
" board[2] = 'O'\n",
" elif move == 7 and board[4] == 'O' and board[8] == 'O'and board[6] == ' ':\n",
" board[6] = 'O'\n",
" elif move == 7 and board[0] == 'O' and board[1] == 'O'and board[2] == ' ':\n",
" board[2] = 'O'\n",
" elif move == 7 and board[0] == 'O' and board[3] == 'O'and board[6] == ' ':\n",
" board[6] = 'O'\n",
" elif move == 7 and board[0] == 'O' and board[8] == 'O'and board[4] == ' ':\n",
" board[4] = 'O'\n",
" elif move == 7 and board[0] == 'O' and board[8] == 'O'and board[2] == ' ':\n",
" board[2] = 'O'\n",
" elif move == 7 and board[0] == 'O' and board[8] == 'O'and board[6] == ' ':\n",
" board[6] = 'O'\n",
" \n",
" elif move == 7 and board[0] == 'X' and board[1] == 'X'and board[2] != 'O':\n",
" board[2] = 'O'\n",
" elif move == 7 and board[1] == 'X' and board[2] == 'X'and board[0] != 'O':\n",
" board[0] = 'O'\n",
" elif move == 7 and board[0] == 'X' and board[2] == 'X'and board[1] != 'O':\n",
" board[1] = 'O'\n",
" elif move == 7 and board[3] == 'X' and board[4] == 'X'and board[5] != 'O':\n",
" board[5] = 'O'\n",
" elif move == 7 and board[4] == 'X' and board[5] == 'X'and board[3] != 'O':\n",
" board[3] = 'O'\n",
" elif move == 7 and board[3] == 'X' and board[5] == 'X'and board[4] != 'O':\n",
" board[4] = 'O'\n",
" elif move == 7 and board[6] == 'X' and board[7] == 'X'and board[8] != 'O':\n",
" board[8] = 'O'\n",
" elif move == 7 and board[7] == 'X' and board[8] == 'X'and board[6] != 'O':\n",
" board[6] = 'O'\n",
" elif move == 7 and board[6] == 'X' and board[8] == 'X'and board[7] != 'O':\n",
" board[7] = 'O'\n",
" elif move == 7 and board[0] == 'X' and board[3] == 'X'and board[6] != 'O':\n",
" board[6] = 'O'\n",
" elif move == 7 and board[3] == 'X' and board[6] == 'X'and board[0] != 'O':\n",
" board[0] = 'O'\n",
" elif move == 7 and board[0] == 'X' and board[6] == 'X'and board[3] != 'O':\n",
" board[3] = 'O'\n",
" elif move == 7 and board[1] == 'X' and board[4] == 'X'and board[7] != 'O':\n",
" board[7] = 'O'\n",
" elif move == 7 and board[1] == 'X' and board[7] == 'X'and board[4] != 'O':\n",
" board[4] = 'O'\n",
" elif move == 7 and board[4] == 'X' and board[7] == 'X'and board[1] != 'O':\n",
" board[1] = 'O'\n",
" elif move == 7 and board[2] == 'X' and board[5] == 'X'and board[8] != 'O':\n",
" board[8] = 'O'\n",
" elif move == 7 and board[5] == 'X' and board[8] == 'X'and board[2] != 'O':\n",
" board[2] = 'O' \n",
" elif move == 7 and board[2] == 'X' and board[8] == 'X'and board[5] != 'O':\n",
" board[5] = 'O' \n",
" elif move == 7 and board[0] == 'X' and board[4] == 'X'and board[8] != 'O':\n",
" board[8] = 'O' \n",
" elif move == 7 and board[0] == 'X' and board[8] == 'X'and board[4] != 'O':\n",
" board[4] = 'O' \n",
" elif move == 7 and board[4] == 'X' and board[8] == 'X'and board[0] != 'O':\n",
" board[0] = 'O' \n",
" elif move == 7 and board[2] == 'X' and board[4] == 'X'and board[6] != 'O':\n",
" board[6] = 'O' \n",
" elif move == 7 and board[2] == 'X' and board[6] == 'X'and board[4] != 'O':\n",
" board[4] = 'O' \n",
" elif move == 7 and board[4] == 'X' and board[6] == 'X'and board[2] != 'O':\n",
" board[2] = 'O'\n",
" \n",
" elif move == 7 and board[1] == ' ':\n",
" board[1] = 'O'\n",
" elif move == 7 and board[2] == ' ':\n",
" board[2] = 'O'\n",
" elif move == 7 and board[3] == ' ':\n",
" board[3] = 'O'\n",
" elif move == 7 and board[4] == ' ':\n",
" board[4] = 'O'\n",
" elif move == 7 and board[5] == ' ':\n",
" board[5] = 'O'\n",
" elif move == 7 and board[6] == ' ':\n",
" board[6] = 'O'\n",
" elif move == 7 and board[7] == ' ':\n",
" board[7] = 'O'\n",
" elif move == 7 and board[8] == ' ':\n",
" board[8] = 'O'"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# E: Nick Knockeart"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def player1_e(move, board):\n",
" if move == 0:\n",
" board[6] = 'X'\n",
" if move == 2 and board[4] == 'O':\n",
" board[2] = 'X'\n",
" elif move == 2 and board[0] == 'O':\n",
" board[8] = 'X'\n",
" elif move == 2 and board[3] == 'O':\n",
" board[8] = 'X'\n",
" elif move == 2 and board[5] == 'O':\n",
" board[8] = 'X'\n",
" elif move == 2 and board[2] == 'O':\n",
" board[8] = 'X'\n",
" elif move == 2 and board[1] == 'O':\n",
" board[0] = 'X'\n",
" elif move == 2 and board[7] == 'O':\n",
" board[0] = 'X'\n",
" elif move == 2 and board[8] == 'O':\n",
" board[0] = 'X'\n",
" if move == 4 and board[4] == 'O' and board[0] == 'O':\n",
" board[8] = 'X'\n",
" elif move == 4 and board[4] == 'O' and board[8] == 'O':\n",
" board[0] = 'X'\n",
" elif move == 6 and board[4] == 'O' and board[8] == 'O' and board[1] == ' ':\n",
" board[1] = 'X'\n",
" elif move == 6 and board[4] == 'O' and board[8] == 'O' and board[3] == ' ':\n",
" board[3] = 'X'\n",
" elif move == 6 and board[4] == 'O' and board[0] == 'O' and board[5] == ' ':\n",
" board[7] = 'X'\n",
" elif move == 6 and board[4] == 'O' and board[0] == 'O' and board[7] == ' ':\n",
" board[5] = 'X'\n",
" if move == 4 and board[4] == 'O' and board[1] == 'O':\n",
" board[7] = 'X'\n",
" if move == 6 and board[4] == 'O' and board[1] == 'O' and board[8] == ' ':\n",
" board[8] = 'X'\n",
" elif move == 6 and board[4] == 'O' and board[1] == 'O' and board[8] == 'O':\n",
" board[0] = 'X'\n",
" if move == 8 and board[4] == 'O' and board[1] == 'O' and board[8] == 'O' and board[3] == ' ':\n",
" board[3] = 'X'\n",
" elif move == 8 and board[4] == 'O' and board[1] == 'O' and board[8] == 'O' and board[3] == 'O':\n",
" board[5] = 'X'\n",
" if move == 4 and board[4] == 'O' and board[3] == 'O':\n",
" board[5] = 'X'\n",
" if move == 6 and board[4] == 'O' and board[3] == 'O' and board[8] == ' ':\n",
" board[8] = 'X'\n",
" elif move == 6 and board[4] == 'O' and board[3] == 'O' and board[8] == 'O':\n",
" board[0] = 'X'\n",
" if move == 8 and board[4] == 'O' and board[3] == 'O' and board[8] == 'O' and board[1] == ' ':\n",
" board[1] = 'X'\n",
" elif move == 8 and board[4] == 'O' and board[3] == 'O' and board[8] == 'O' and board[1] == 'O':\n",
" board[7] = 'X'\n",
" if move == 4 and board[4] == 'O' and board[5] == 'O':\n",
" board[3] = 'X'\n",
" if move == 6 and board[4] == 'O' and board[5] == 'O' and board[0] == ' ':\n",
" board[0] = 'X'\n",
" elif move == 6 and board[4] == 'O' and board[5] == 'O' and board[0] == 'O':\n",
" board[8] = 'X'\n",
" if move == 8 and board[4] == 'O' and board[5] == 'O' and board[0] == 'O' and board[7] == ' ':\n",
" board[7] = 'X'\n",
" elif move == 8 and board[4] == 'O' and board[5] == 'O' and board[0] == 'O' and board[7] == 'O':\n",
" board[1] = 'X'\n",
" if move == 4 and board[4] == 'O' and board[7] == 'O':\n",
" board[1] = 'X'\n",
" if move == 6 and board[4] == 'O' and board[7] == 'O' and board[0] == ' ':\n",
" board[0] = 'X'\n",
" elif move == 6 and board[4] == 'O' and board[7] == 'O' and board[0] == 'O':\n",
" board[8] = 'X'\n",
" if move == 8 and board[4] == 'O' and board[7] == 'O' and board[0] == 'O' and board[5] == ' ':\n",
" board[5] = 'X'\n",
" elif move == 8 and board[4] == 'O' and board[7] == 'O' and board[0] == 'O' and board[5] == 'O':\n",
" board[3] = 'X'\n",
" if move == 4 and board[3] == 'O' and board[7] == 'O':\n",
" board[2] = 'X'\n",
" elif move == 4 and board[3] == 'O' and board[7] == ' ':\n",
" board[7] = 'X'\n",
" if move == 6 and board[3] == 'O' and board[7] == 'O' and board[4] == ' ':\n",
" board[5] = 'X'\n",
" elif move == 6 and board[3] == 'O' and board[7] == 'O' and board[4] == ' ':\n",
" board[4] = 'X'\n",
" elif move == 6 and board[3] == 'O' and board[7] == 'O' and board[0] == 'O':\n",
" board[4] = 'X'\n",
" elif move == 6 and board[3] == 'O' and board[7] == 'O' and board[1] == 'O':\n",
" board[4] = 'X'\n",
" if move == 4 and board[5] == 'O' and board[7] == ' ':\n",
" board[7] = 'X'\n",
" elif move == 4 and board[5] == 'O' and board[7] == 'O':\n",
" board[4] = 'X'\n",
" if move == 6 and board[5] == 'O' and board[7] == 'O' and board[3] == 'O':\n",
" board[0] = 'X'\n",
" elif move == 6 and board[5] == 'O' and board[7] == 'O' and board[3] == 'O':\n",
" board[0] = 'X'\n",
" elif move == 6 and board[5] == 'O' and board[7] == 'O' and board[0] == 'O':\n",
" board[2] = 'X'\n",
" elif move == 6 and board[5] == 'O' and board[7] == 'O' and board[1] == 'O':\n",
" board[0] = 'X'\n",
" elif move == 6 and board[5] == 'O' and board[7] == 'O' and board[2] == 'O':\n",
" board[0] = 'X'\n",
" if move == 4 and board[2] == 'O' and board[7] == ' ':\n",
" board[7] = 'X'\n",
" elif move == 4 and board[2] == 'O' and board[7] == 'O':\n",
" board[0] = 'X'\n",
" if move == 6 and board[2] == 'O' and board[7] == 'O' and board[1] == 'O':\n",
" board[3] = 'X'\n",
" elif move == 6 and board[2] == 'O' and board[7] == 'O' and board[3] == 'O':\n",
" board[4] = 'X'\n",
" elif move == 6 and board[2] == 'O' and board[7] == 'O' and board[4] == 'O':\n",
" board[3] = 'X'\n",
" elif move == 6 and board[2] == 'O' and board[7] == 'O' and board[5] == 'O':\n",
" board[3] = 'X'\n",
" if move == 4 and board[1] == 'O' and board[3] == 'O':\n",
" board[8] = 'X'\n",
" elif move == 4 and board[1] == 'O' and board[3] == ' ':\n",
" board[3] = 'X'\n",
" if move == 6 and board[1] == 'O' and board[3] == 'O' and board[7] == 'O':\n",
" board[4] = 'X'\n",
" elif move == 6 and board[1] == 'O' and board[3] == 'O' and board[2] == 'O':\n",
" board[4] = 'X'\n",
" elif move == 6 and board[1] == 'O' and board[3] == 'O' and board[5] == 'O':\n",
" board[4] = 'X'\n",
" elif move == 6 and board[1] == 'O' and board[3] == 'O' and board[4] == 'O':\n",
" board[7] = 'X'\n",
" if move == 4 and board[8] == 'O' and board[3] == 'O':\n",
" board[2] = 'X'\n",
" elif move == 4 and board[8] == 'O' and board[3] == ' ':\n",
" board[3] = 'X'\n",
" if move == 6 and board[8] == 'O' and board[3] == 'O' and board[1] == 'O':\n",
" board[4] = 'X'\n",
" elif move == 6 and board[8] == 'O' and board[3] == 'O' and board[5] == 'O':\n",
" board[4] = 'X'\n",
" elif move == 6 and board[8] == 'O' and board[3] == 'O' and board[7] == 'O':\n",
" board[4] = 'X'\n",
" elif move == 6 and board[8] == 'O' and board[3] == 'O' and board[4] == 'O':\n",
" board[7] = 'X'\n",
" if move == 4 and board[7] == 'O' and board[3] == 'O':\n",
" board[2] = 'X'\n",
" elif move == 4 and board[7] == 'O' and board[3] == ' ':\n",
" board[3] = 'X'\n",
" if move == 6 and board[7] == 'O' and board[3] == 'O' and board[1] == 'O':\n",
" board[4] = 'X'\n",
" elif move == 6 and board[7] == 'O' and board[3] == 'O' and board[5] == 'O':\n",
" board[4] = 'X'\n",
" elif move == 6 and board[7] == 'O' and board[3] == 'O' and board[8] == 'O':\n",
" board[4] = 'X'\n",
" elif move == 6 and board[7] == 'O' and board[3] == 'O' and board[4] == 'O':\n",
" board[1] = 'X'\n",
" if move == 4 and board[0] == 'O' and board[7] == 'O':\n",
" board[2] = 'X'\n",
" elif move == 4 and board[0] == 'O' and board[7] == ' ':\n",
" board[3] = 'X'\n",
" if move == 6 and board[0] == 'O' and board[7] == 'O' and board[1] == 'O':\n",
" board[4] = 'X'\n",
" elif move == 6 and board[0] == 'O' and board[7] == 'O' and board[5] == 'O':\n",
" board[4] = 'X'\n",
" elif move == 6 and board[0] == 'O' and board[7] == 'O' and board[3] == 'O':\n",
" board[4] = 'X'\n",
" elif move == 6 and board[0] == 'O' and board[7] == 'O' and board[4] == 'O':\n",
" board[5] = 'X'"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def player2_e(move, board):\n",
" if move == 1 and board[4] == 'X':\n",
" board[0] = 'O'\n",
" elif move == 1 and board[0] == 'X':\n",
" board[4] = 'O'\n",
" if move == 3 and board[4] == 'O' and board[0] == 'X' and board[1] == 'X':\n",
" board[2] = 'O'\n",
" if move == 5 and board[4] == 'O' and board[0] == 'X' and board[1] == 'X' and board[6] == ' ':\n",
" board[6] = 'O'\n",
" elif move == 5 and board[4] == 'O' and board[0] == 'X' and board[1] == 'X' and board[6] == 'X':\n",
" board[3] = 'O'\n",
" if move == 7 and board[4] == 'O' and board[0] == 'X' and board[1] == 'X' and board[6] == 'X' and board[5] == ' ':\n",
" board[5] = 'O'\n",
" elif move == 7 and board[4] == 'O' and board[0] == 'X' and board[1] == 'X' and board[6] == 'X' and board[5] == 'X':\n",
" board[7] = 'O'\n",
" if move == 3 and board[4] == 'O' and board[0] == 'X' and board[3] == 'X':\n",
" board[6] = 'O'\n",
" if move == 5 and board[4] == 'O' and board[0] == 'X' and board[3] == 'X' and board[2] == ' ':\n",
" board[2] = 'O'\n",
" elif move == 5 and board[4] == 'O' and board[0] == 'X' and board[3] == 'X' and board[2] == 'X':\n",
" board[1] = 'O'\n",
" if move == 7 and board[4] == 'O' and board[0] == 'X' and board[3] == 'X' and board[2] == 'X' and board[7] == ' ':\n",
" board[7] = 'O'\n",
" elif move == 7 and board[4] == 'O' and board[0] == 'X' and board[3] == 'X' and board[2] == 'X' and board[7] == 'X':\n",
" board[5] = 'O'\n",
" if move == 3 and board[4] == 'O' and board[0] == 'X' and board[2] == 'X':\n",
" board[1] = 'O'\n",
" if move == 5 and board[4] == 'O' and board[0] == 'X' and board[1] == 'X' and board[6] == ' ':\n",
" board[6] = 'O'\n",
" elif move == 5 and board[4] == 'O' and board[0] == 'X' and board[1] == 'X' and board[6] == 'X':\n",
" board[3] = 'O'\n",
" if move == 7 and board[4] == 'O' and board[0] == 'X' and board[1] == 'X' and board[6] == 'X' and board[5] == ' ':\n",
" board[5] = 'O'\n",
" elif move == 7 and board[4] == 'O' and board[0] == 'X' and board[1] == 'X' and board[6] == 'X' and board[5] == 'X':\n",
" board[8] = 'O'\n",
" if move == 3 and board[4] == 'O' and board[0] == 'X' and board[5] == 'X':\n",
" board[1] = 'O'\n",
" if move == 5 and board[4] == 'O' and board[0] == 'X' and board[1] == 'X' and board[7] == ' ':\n",
" board[7] = 'O'\n",
" elif move == 5 and board[4] == 'O' and board[0] == 'X' and board[1] == 'X' and board[7] == 'X':\n",
" board[6] = 'O'\n",
" if move == 7 and board[4] == 'O' and board[0] == 'X' and board[1] == 'X' and board[7] == 'X' and board[2] == ' ':\n",
" board[2] = 'O'\n",
" elif move == 7 and board[4] == 'O' and board[0] == 'X' and board[1] == 'X' and board[7] == 'X' and board[2] == 'X':\n",
" board[8] = 'O'\n",
" if move == 3 and board[4] == 'O' and board[0] == 'X' and board[6] == 'X':\n",
" board[3] = 'O'\n",
" if move == 5 and board[4] == 'O' and board[0] == 'X' and board[6] == 'X' and board[5] == ' ':\n",
" board[5] = 'O'\n",
" elif move == 5 and board[4] == 'O' and board[0] == 'X' and board[6] == 'X' and board[5] == 'X':\n",
" board[1] = 'O'\n",
" if move == 7 and board[4] == 'O' and board[0] == 'X' and board[6] == 'X' and board[5] == 'X' and board[7] == ' ':\n",
" board[7] = 'O'\n",
" elif move == 7 and board[4] == 'O' and board[0] == 'X' and board[6] == 'X' and board[5] == 'X' and board[7] == 'X':\n",
" board[8] = 'O'\n",
" if move == 3 and board[4] == 'O' and board[0] == 'X' and board[7] == 'X':\n",
" board[3] = 'O'\n",
" if move == 5 and board[4] == 'O' and board[0] == 'X' and board[7] == 'X' and board[5] == ' ':\n",
" board[5] = 'O'\n",
" elif move == 5 and board[4] == 'O' and board[0] == 'X' and board[7] == 'X' and board[5] == 'X':\n",
" board[2] = 'O'\n",
" if move == 7 and board[4] == 'O' and board[0] == 'X' and board[7] == 'X' and board[5] == 'X' and board[6] == ' ':\n",
" board[6] = 'O'\n",
" elif move == 7 and board[4] == 'O' and board[0] == 'X' and board[7] == 'X' and board[5] == 'X' and board[6] == 'X':\n",
" board[6] = 'O'\n",
" if move == 3 and board[4] == 'O' and board[0] == 'X' and board[8] == 'X':\n",
" board[1] = 'O'\n",
" if move == 5 and board[4] == 'O' and board[0] == 'X' and board[8] == 'X' and board[7] == ' ':\n",
" board[7] = 'O'\n",
" elif move == 5 and board[4] == 'O' and board[0] == 'X' and board[6] == 'X' and board[7] == 'X':\n",
" board[6] = 'O'\n",
" if move == 7 and board[4] == 'O' and board[0] == 'X' and board[6] == 'X' and board[7] == 'X' and board[2] == ' ':\n",
" board[2] = 'O'\n",
" elif move == 7 and board[4] == 'O' and board[0] == 'X' and board[6] == 'X' and board[7] == 'X' and board[2] == 'X':\n",
" board[5] = 'O'\n",
" elif move == 1 and board[1] == 'X':\n",
" board[4] = 'O'\n",
" if move == 3 and board[4] == 'O' and board[1] == 'X' and board[2] == 'X':\n",
" board[0] = 'O'\n",
" if move == 5 and board[4] == 'O' and board[1] == 'X' and board[2] == 'X' and board[8] == ' ':\n",
" board[8] = 'O'\n",
" elif move == 5 and board[4] == 'O' and board[1] == 'X' and board[2] == 'X' and board[8] == 'X':\n",
" board[5] = 'O'\n",
" if move == 7 and board[4] == 'O' and board[1] == 'X' and board[2] == 'X' and board[8] == 'X' and board[3] == ' ':\n",
" board[3] = 'O'\n",
" elif move == 7 and board[4] == 'O' and board[1] == 'X' and board[2] == 'X' and board[8] == 'X' and board[3] == 'X':\n",
" board[7] = 'O'\n",
" if move == 3 and board[4] == 'O' and board[1] == 'X' and board[3] == 'X':\n",
" board[0] = 'O'\n",
" if move == 5 and board[4] == 'O' and board[1] == 'X' and board[3] == 'X' and board[8] == ' ':\n",
" board[8] = 'O'\n",
" elif move == 5 and board[4] == 'O' and board[1] == 'X' and board[3] == 'X' and board[8] == 'X':\n",
" board[6] = 'O'\n",
" if move == 7 and board[4] == 'O' and board[1] == 'X' and board[3] == 'X' and board[8] == 'X' and board[2] == ' ':\n",
" board[2] = 'O'\n",
" elif move == 7 and board[4] == 'O' and board[1] == 'X' and board[3] == 'X' and board[8] == 'X' and board[2] == 'X':\n",
" board[5] = 'O'\n",
" if move == 3 and board[4] == 'O' and board[1] == 'X' and board[5] == 'X':\n",
" board[2] = 'O'\n",
" if move == 5 and board[4] == 'O' and board[1] == 'X' and board[5] == 'X' and board[6] == ' ':\n",
" board[6] = 'O'\n",
" elif move == 5 and board[4] == 'O' and board[1] == 'X' and board[5] == 'X' and board[6] == 'X':\n",
" board[0] = 'O'\n",
" if move == 7 and board[4] == 'O' and board[1] == 'X' and board[5] == 'X' and board[6] == 'X' and board[8] == ' ':\n",
" board[8] = 'O'\n",
" elif move == 7 and board[4] == 'O' and board[1] == 'X' and board[5] == 'X' and board[6] == 'X' and board[8] == 'X':\n",
" board[3] = 'O'\n",
" if move == 3 and board[4] == 'O' and board[1] == 'X' and board[6] == 'X':\n",
" board[0] = 'O'\n",
" if move == 5 and board[4] == 'O' and board[1] == 'X' and board[6] == 'X' and board[8] == ' ':\n",
" board[6] = 'O'\n",
" elif move == 5 and board[4] == 'O' and board[1] == 'X' and board[6] == 'X' and board[8] == 'X':\n",
" board[7] = 'O'\n",
" if move == 7 and board[4] == 'O' and board[1] == 'X' and board[6] == 'X' and board[8] == 'X' and board[5] == ' ':\n",
" board[5] = 'O'\n",
" elif move == 7 and board[4] == 'O' and board[1] == 'X' and board[6] == 'X' and board[8] == 'X' and board[5] == 'X':\n",
" board[2] = 'O'\n",
" if move == 3 and board[4] == 'O' and board[1] == 'X' and board[7] == 'X':\n",
" board[0] = 'O'\n",
" if move == 5 and board[4] == 'O' and board[1] == 'X' and board[7] == 'X' and board[8] == ' ':\n",
" board[8] = 'O'\n",
" elif move == 5 and board[4] == 'O' and board[1] == 'X' and board[7] == 'X' and board[8] == 'X':\n",
" board[6] = 'O'\n",
" if move == 7 and board[4] == 'O' and board[1] == 'X' and board[7] == 'X' and board[8] == 'X' and board[2] == ' ':\n",
" board[2] = 'O'\n",
" elif move == 7 and board[4] == 'O' and board[1] == 'X' and board[7] == 'X' and board[8] == 'X' and board[2] == 'X':\n",
" board[3] = 'O'\n",
" if move == 3 and board[4] == 'O' and board[1] == 'X' and board[8] == 'X':\n",
" board[2] = 'O'\n",
" if move == 5 and board[4] == 'O' and board[1] == 'X' and board[8] == 'X' and board[6] == ' ':\n",
" board[8] = 'O'\n",
" elif move == 5 and board[4] == 'O' and board[1] == 'X' and board[8] == 'X' and board[6] == 'X':\n",
" board[7] = 'O'\n",
" if move == 7 and board[4] == 'O' and board[1] == 'X' and board[8] == 'X' and board[6] == 'X' and board[3] == ' ':\n",
" board[3] = 'O'\n",
" elif move == 7 and board[4] == 'O' and board[1] == 'X' and board[8] == 'X' and board[6] == 'X' and board[3] == 'X':\n",
" board[0] = 'O'\n",
" elif move == 1 and board[2] == 'X':\n",
" board[4] = 'O'\n",
" if move == 3 and board[4] == 'O' and board[2] == 'X' and board[3] == 'X':\n",
" board[0] = 'O'\n",
" if move == 5 and board[4] == 'O' and board[2] == 'X' and board[3] == 'X' and board[8] == ' ':\n",
" board[8] = 'O'\n",
" elif move == 5 and board[4] == 'O' and board[2] == 'X' and board[3] == 'X' and board[8] == 'X':\n",
" board[5] = 'O'\n",
" if move == 7 and board[4] == 'O' and board[2] == 'X' and board[3] == 'X' and board[8] == 'X' and board[7] == ' ':\n",
" board[7] = 'O'\n",
" elif move == 7 and board[4] == 'O' and board[2] == 'X' and board[3] == 'X' and board[8] == 'X' and board[7] == 'X':\n",
" board[6] = 'O'\n",
" if move == 3 and board[4] == 'O' and board[2] == 'X' and board[5] == 'X':\n",
" board[8] = 'O'\n",
" if move == 5 and board[4] == 'O' and board[2] == 'X' and board[5] == 'X' and board[0] == ' ':\n",
" board[0] = 'O'\n",
" elif move == 5 and board[4] == 'O' and board[2] == 'X' and board[5] == 'X' and board[0] == 'X':\n",
" board[1] = 'O'\n",
" if move == 7 and board[4] == 'O' and board[2] == 'X' and board[5] == 'X' and board[0] == 'X' and board[7] == ' ':\n",
" board[7] = 'O'\n",
" elif move == 7 and board[4] == 'O' and board[2] == 'X' and board[5] == 'X' and board[0] == 'X' and board[7] == 'X':\n",
" board[6] = 'O'\n",
" if move == 3 and board[4] == 'O' and board[2] == 'X' and board[6] == 'X':\n",
" board[1] = 'O'\n",
" if move == 5 and board[4] == 'O' and board[2] == 'X' and board[6] == 'X' and board[7] == ' ':\n",
" board[7] = 'O'\n",
" elif move == 5 and board[4] == 'O' and board[2] == 'X' and board[6] == 'X' and board[7] == 'X':\n",
" board[8] = 'O'\n",
" if move == 7 and board[4] == 'O' and board[2] == 'X' and board[6] == 'X' and board[7] == 'X' and board[0] == ' ':\n",
" board[0] = 'O'\n",
" elif move == 7 and board[4] == 'O' and board[2] == 'X' and board[6] == 'X' and board[7] == 'X' and board[0] == 'X':\n",
" board[3] = 'O'\n",
" if move == 3 and board[4] == 'O' and board[2] == 'X' and board[7] == 'X':\n",
" board[8] = 'O'\n",
" if move == 5 and board[4] == 'O' and board[2] == 'X' and board[7] == 'X' and board[0] == ' ':\n",
" board[0] = 'O'\n",
" elif move == 5 and board[4] == 'O' and board[2] == 'X' and board[7] == 'X' and board[0] == 'X':\n",
" board[1] = 'O'\n",
" if move == 7 and board[4] == 'O' and board[2] == 'X' and board[7] == 'X' and board[0] == 'X' and board[3] == ' ':\n",
" board[3] = 'O'\n",
" elif move == 7 and board[4] == 'O' and board[2] == 'X' and board[7] == 'X' and board[0] == 'X' and board[3] == 'X':\n",
" board[6] = 'O'\n",
" if move == 3 and board[4] == 'O' and board[2] == 'X' and board[8] == 'X':\n",
" board[5] = 'O'\n",
" if move == 5 and board[4] == 'O' and board[2] == 'X' and board[8] == 'X' and board[3] == ' ':\n",
" board[3] = 'O'\n",
" elif move == 5 and board[4] == 'O' and board[2] == 'X' and board[8] == 'X' and board[3] == 'X':\n",
" board[1] = 'O'\n",
" if move == 7 and board[4] == 'O' and board[2] == 'X' and board[8] == 'X' and board[3] == 'X' and board[7] == ' ':\n",
" board[7] = 'O'\n",
" elif move == 7 and board[4] == 'O' and board[2] == 'X' and board[8] == 'X' and board[3] == 'X' and board[7] == 'X':\n",
" board[6] = 'O'\n",
" elif move == 1 and board[3] == 'X':\n",
" board[4] = 'O'\n",
" if move == 3 and board[4] == 'O' and board[3] == 'X' and board[5] == 'X':\n",
" board[0] = 'O'\n",
" if move == 5 and board[4] == 'O' and board[3] == 'X' and board[5] == 'X' and board[8] == ' ':\n",
" board[8] = 'O'\n",
" elif move == 5 and board[4] == 'O' and board[3] == 'X' and board[5] == 'X' and board[8] == 'X':\n",
" board[2] = 'O'\n",
" if move == 7 and board[4] == 'O' and board[3] == 'X' and board[5] == 'X' and board[8] == 'X' and board[6] == ' ':\n",
" board[6] = 'O'\n",
" elif move == 7 and board[4] == 'O' and board[3] == 'X' and board[5] == 'X' and board[8] == 'X' and board[6] == 'X':\n",
" board[1] = 'O'\n",
" if move == 3 and board[4] == 'O' and board[3] == 'X' and board[6] == 'X':\n",
" board[0] = 'O'\n",
" if move == 5 and board[4] == 'O' and board[3] == 'X' and board[6] == 'X' and board[8] == ' ':\n",
" board[8] = 'O'\n",
" elif move == 5 and board[4] == 'O' and board[3] == 'X' and board[6] == 'X' and board[8] == 'X':\n",
" board[7] = 'O'\n",
" if move == 7 and board[4] == 'O' and board[3] == 'X' and board[6] == 'X' and board[8] == 'X' and board[1] == ' ':\n",
" board[1] = 'O'\n",
" elif move == 7 and board[4] == 'O' and board[3] == 'X' and board[6] == 'X' and board[8] == 'X' and board[1] == 'X':\n",
" board[2] = 'O'\n",
" if move == 3 and board[4] == 'O' and board[3] == 'X' and board[7] == 'X':\n",
" board[6] = 'O'\n",
" if move == 5 and board[4] == 'O' and board[3] == 'X' and board[7] == 'X' and board[2] == ' ':\n",
" board[2] = 'O'\n",
" elif move == 5 and board[4] == 'O' and board[3] == 'X' and board[7] == 'X' and board[2] == 'X':\n",
" board[0] = 'O'\n",
" if move == 7 and board[4] == 'O' and board[3] == 'X' and board[7] == 'X' and board[2] == 'X' and board[8] == ' ':\n",
" board[8] = 'O'\n",
" elif move == 7 and board[4] == 'O' and board[3] == 'X' and board[7] == 'X' and board[2] == 'X' and board[8] == 'X':\n",
" board[5] = 'O'\n",
" if move == 3 and board[4] == 'O' and board[3] == 'X' and board[8] == 'X':\n",
" board[6] = 'O'\n",
" if move == 5 and board[4] == 'O' and board[3] == 'X' and board[8] == 'X' and board[2] == ' ':\n",
" board[2] = 'O'\n",
" elif move == 5 and board[4] == 'O' and board[3] == 'X' and board[8] == 'X' and board[2] == 'X':\n",
" board[5] = 'O'\n",
" if move == 7 and board[4] == 'O' and board[3] == 'X' and board[8] == 'X' and board[2] == 'X' and board[1] == ' ':\n",
" board[1] = 'O'\n",
" elif move == 7 and board[4] == 'O' and board[3] == 'X' and board[8] == 'X' and board[2] == 'X' and board[1] == 'X':\n",
" board[0] = 'O'\n",
" elif move == 1 and board[5] == 'X':\n",
" board[4] = 'O'\n",
" if move == 3 and board[4] == 'O' and board[5] == 'X' and board[6] == 'X':\n",
" board[8] = 'O'\n",
" if move == 5 and board[4] == 'O' and board[5] == 'X' and board[6] == 'X' and board[0] == ' ':\n",
" board[0] = 'O'\n",
" elif move == 5 and board[4] == 'O' and board[5] == 'X' and board[6] == 'X' and board[0] == 'X':\n",
" board[3] = 'O'\n",
" if move == 7 and board[4] == 'O' and board[5] == 'X' and board[6] == 'X' and board[0] == 'X' and board[1] == ' ':\n",
" board[1] = 'O'\n",
" elif move == 7 and board[4] == 'O' and board[5] == 'X' and board[6] == 'X' and board[0] == 'X' and board[1] == 'X':\n",
" board[2] = 'O'\n",
" if move == 3 and board[4] == 'O' and board[5] == 'X' and board[7] == 'X':\n",
" board[8] = 'O'\n",
" if move == 5 and board[4] == 'O' and board[5] == 'X' and board[7] == 'X' and board[0] == ' ':\n",
" board[0] = 'O'\n",
" elif move == 5 and board[4] == 'O' and board[5] == 'X' and board[7] == 'X' and board[0] == 'X':\n",
" board[2] = 'O'\n",
" if move == 7 and board[4] == 'O' and board[5] == 'X' and board[7] == 'X' and board[0] == 'X' and board[6] == ' ':\n",
" board[6] = 'O'\n",
" elif move == 7 and board[4] == 'O' and board[5] == 'X' and board[7] == 'X' and board[0] == 'X' and board[6] == 'X':\n",
" board[3] = 'O'\n",
" if move == 3 and board[4] == 'O' and board[5] == 'X' and board[8] == 'X':\n",
" board[2] = 'O'\n",
" if move == 5 and board[4] == 'O' and board[5] == 'X' and board[8] == 'X' and board[6] == ' ':\n",
" board[6] = 'O'\n",
" elif move == 5 and board[4] == 'O' and board[5] == 'X' and board[8] == 'X' and board[6] == 'X':\n",
" board[7] = 'O'\n",
" if move == 7 and board[4] == 'O' and board[5] == 'X' and board[8] == 'X' and board[6] == 'X' and board[1] == ' ':\n",
" board[1] = 'O'\n",
" elif move == 7 and board[4] == 'O' and board[5] == 'X' and board[8] == 'X' and board[6] == 'X' and board[1] == 'X':\n",
" board[3] = 'O'\n",
" elif move == 1 and board[6] == 'X':\n",
" board[4] = 'O'\n",
" if move == 3 and board[4] == 'O' and board[6] == 'X' and board[7] == 'X':\n",
" board[8] = 'O'\n",
" if move == 5 and board[4] == 'O' and board[6] == 'X' and board[7] == 'X' and board[0] == ' ':\n",
" board[0] = 'O'\n",
" elif move == 5 and board[4] == 'O' and board[6] == 'X' and board[7] == 'X' and board[0] == 'X':\n",
" board[3] = 'O'\n",
" if move == 7 and board[4] == 'O' and board[6] == 'X' and board[7] == 'X' and board[0] == 'X' and board[5] == ' ':\n",
" board[5] = 'O'\n",
" elif move == 7 and board[4] == 'O' and board[6] == 'X' and board[7] == 'X' and board[0] == 'X' and board[5] == 'X':\n",
" board[1] = 'O'\n",
" if move == 3 and board[4] == 'O' and board[6] == 'X' and board[8] == 'X':\n",
" board[7] = 'O'\n",
" if move == 5 and board[4] == 'O' and board[6] == 'X' and board[8] == 'X' and board[1] == ' ':\n",
" board[1] = 'O'\n",
" elif move == 5 and board[4] == 'O' and board[6] == 'X' and board[8] == 'X' and board[1] == 'X':\n",
" board[3] = 'O'\n",
" if move == 7 and board[4] == 'O' and board[6] == 'X' and board[8] == 'X' and board[1] == 'X' and board[5] == ' ':\n",
" board[5] = 'O'\n",
" elif move == 7 and board[4] == 'O' and board[6] == 'X' and board[8] == 'X' and board[1] == 'X' and board[5] == 'X':\n",
" board[2] = 'O'\n",
" elif move == 1 and board[7] == 'X':\n",
" board[4] = 'O'\n",
" if move == 3 and board[4] == 'O' and board[7] == 'X' and board[8] == 'X':\n",
" board[6] = 'O'\n",
" if move == 5 and board[4] == 'O' and board[7] == 'X' and board[8] == 'X' and board[2] == ' ':\n",
" board[2] = 'O'\n",
" elif move == 5 and board[4] == 'O' and board[7] == 'X' and board[8] == 'X' and board[2] == 'X':\n",
" board[5] = 'O'\n",
" if move == 7 and board[4] == 'O' and board[7] == 'X' and board[8] == 'X' and board[2] == 'X' and board[3] == ' ':\n",
" board[3] = 'O'\n",
" elif move == 7 and board[4] == 'O' and board[7] == 'X' and board[8] == 'X' and board[2] == 'X' and board[3] == 'X':\n",
" board[1] = 'O'\n",
" elif move == 1 and board[8] == 'X':\n",
" board[4] = 'O'\n",
" if move == 3 and board[4] == 'X' and board[1] == 'X':\n",
" board[7] = 'O'\n",
" if move == 5 and board[4] == 'X' and board[1] == 'X' and board[2] == 'X':\n",
" board[6] = 'O'\n",
" elif move == 5 and board[4] == 'X' and board[1] == 'X' and board[2] == 'X':\n",
" board[5] = 'O'\n",
" if move == 7 and board[4] == 'X' and board[1] == 'X' and board[2] == 'X' and board[3] == ' ':\n",
" board[3] = 'O'\n",
" elif move == 7 and board[4] == 'X' and board[1] == 'X' and board[2] == 'X' and board[3] == 'X':\n",
" board[5] = 'O'\n",
" elif move == 5 and board[4] == 'X' and board[1] == 'X' and board[5] == 'X':\n",
" board[3] = 'O'\n",
" if move == 7 and board[4] == 'X' and board[1] == 'X' and board[5] == 'X' and board[6] == ' ':\n",
" board[6] = 'O'\n",
" elif move == 7 and board[4] == 'X' and board[1] == 'X' and board[5] == 'X' and board[6] == 'X':\n",
" board[2] = 'O'\n",
" elif move == 5 and board[4] == 'X' and board[1] == 'X' and board[6] == 'X':\n",
" board[2] = 'O'\n",
" if move == 7 and board[4] == 'X' and board[1] == 'X' and board[6] == 'X' and board[3] == 'X':\n",
" board[5] = 'O'\n",
" elif move == 7 and board[4] == 'X' and board[1] == 'X' and board[6] == 'X' and board[5] == 'X':\n",
" board[3] = 'O'\n",
" elif move == 7 and board[4] == 'X' and board[1] == 'X' and board[6] == 'X' and board[8] == 'X':\n",
" board[3] = 'O'\n",
" elif move == 5 and board[4] == 'X' and board[1] == 'X' and board[8] == 'X':\n",
" board[6] = 'O'\n",
" if move == 7 and board[4] == 'X' and board[1] == 'X' and board[8] == 'X' and board[2] == ' ':\n",
" board[2] = 'O'\n",
" elif move == 7 and board[4] == 'X' and board[1] == 'X' and board[8] == 'X' and board[2] == 'X':\n",
" board[5] = 'O'\n",
" if move == 3 and board[4] == 'X' and board[2] == 'X':\n",
" board[6] = 'O'\n",
" if move == 5 and board[4] == 'X' and board[2] == 'X' and board[3] == ' ':\n",
" board[3] = 'O'\n",
" elif move == 5 and board[4] == 'X' and board[2] == 'X' and board[3] == 'X':\n",
" board[5] = 'O'\n",
" if move == 7 and board[4] == 'X' and board[2] == 'X' and board[3] == 'X' and board[1] == 'X':\n",
" board[7] = 'O'\n",
" elif move == 7 and board[4] == 'X' and board[2] == 'X' and board[3] == 'X' and board[7] == 'X':\n",
" board[1] = 'O'\n",
" elif move == 7 and board[4] == 'X' and board[2] == 'X' and board[3] == 'X' and board[8] == 'X':\n",
" board[1] = 'O'\n",
" if move == 3 and board[4] == 'X' and board[3] == 'X':\n",
" board[5] = 'O'\n",
" if move == 5 and board[4] == 'X' and board[3] == 'X' and board[1] == 'X':\n",
" board[7] = 'O'\n",
" if move == 7 and board[4] == 'X' and board[3] == 'X' and board[1] == 'X' and board[2] == ' ':\n",
" board[2] = 'O'\n",
" elif move == 7 and board[4] == 'X' and board[3] == 'X' and board[1] == 'X' and board[2] == 'X':\n",
" board[6] = 'O'\n",
" elif move == 5 and board[4] == 'X' and board[3] == 'X' and board[2] == 'X':\n",
" board[6] = 'O'\n",
" if move == 7 and board[4] == 'X' and board[3] == 'X' and board[2] == 'X' and board[1] == 'X':\n",
" board[7] = 'O'\n",
" elif move == 7 and board[4] == 'X' and board[3] == 'X' and board[2] == 'X' and board[7] == 'X':\n",
" board[1] = 'O'\n",
" elif move == 7 and board[4] == 'X' and board[3] == 'X' and board[2] == 'X' and board[8] == 'X':\n",
" board[1] = 'O'\n",
" elif move == 5 and board[4] == 'X' and board[3] == 'X' and board[6] == 'X':\n",
" board[2] = 'O'\n",
" if move == 7 and board[4] == 'X' and board[3] == 'X' and board[6] == 'X' and board[1] == ' ':\n",
" board[1] = 'O'\n",
" elif move == 7 and board[4] == 'X' and board[3] == 'X' and board[6] == 'X' and board[1] == 'X':\n",
" board[8] = 'O'\n",
" elif move == 5 and board[4] == 'X' and board[3] == 'X' and board[7] == 'X':\n",
" board[1] = 'O'\n",
" if move == 7 and board[4] == 'X' and board[3] == 'X' and board[7] == 'X' and board[1] == ' ':\n",
" board[1] = 'O'\n",
" elif move == 7 and board[4] == 'X' and board[3] == 'X' and board[7] == 'X' and board[1] == 'X':\n",
" board[6] = 'O'\n",
" elif move == 5 and board[4] == 'X' and board[3] == 'X' and board[8] == 'X':\n",
" board[1] = 'O'\n",
" if move == 7 and board[4] == 'X' and board[3] == 'X' and board[8] == 'X' and board[2] == ' ':\n",
" board[2] = 'O'\n",
" elif move == 7 and board[4] == 'X' and board[3] == 'X' and board[8] == 'X' and board[2] == 'X':\n",
" board[6] = 'O'\n",
" if move == 3 and board[4] == 'X' and board[5] == 'X':\n",
" board[3] = 'O'\n",
" if move == 5 and board[4] == 'X' and board[5] == 'X' and board[6] == ' ':\n",
" board[6] = 'O'\n",
" elif move == 5 and board[4] == 'X' and board[5] == 'X' and board[6] == 'X':\n",
" board[2] = 'O'\n",
" if move == 7 and board[4] == 'X' and board[5] == 'X' and board[6] == 'X' and board[1] == ' ':\n",
" board[1] = 'O'\n",
" elif move == 7 and board[4] == 'X' and board[5] == 'X' and board[6] == 'X' and board[1] == 'X':\n",
" board[7] = 'O'\n",
" if move == 3 and board[4] == 'X' and board[6] == 'X':\n",
" board[2] = 'O'\n",
" if move == 5 and board[4] == 'X' and board[6] == 'X' and board[1] == ' ':\n",
" board[1] = 'O'\n",
" elif move == 5 and board[4] == 'X' and board[6] == 'X' and board[1] == 'X':\n",
" board[7] = 'O'\n",
" if move == 7 and board[4] == 'X' and board[6] == 'X' and board[1] == 'X' and board[3] == 'X':\n",
" board[5] = 'O'\n",
" elif move == 7 and board[4] == 'X' and board[6] == 'X' and board[1] == 'X' and board[5] == 'X':\n",
" board[3] = 'O'\n",
" elif move == 7 and board[4] == 'X' and board[6] == 'X' and board[1] == 'X' and board[8] == 'X':\n",
" board[3] = 'O'\n",
" if move == 3 and board[4] == 'X' and board[7] == 'X':\n",
" board[1] = 'O'\n",
" if move == 5 and board[4] == 'X' and board[7] == 'X' and board[2] == ' ':\n",
" board[2] = 'O'\n",
" elif move == 5 and board[4] == 'X' and board[7] == 'X' and board[2] == 'X':\n",
" board[6] = 'O'\n",
" if move == 7 and board[4] == 'X' and board[7] == 'X' and board[2] == 'X' and board[3] == ' ':\n",
" board[3] = 'O'\n",
" elif move == 7 and board[4] == 'X' and board[7] == 'X' and board[2] == 'X' and board[3] == 'X':\n",
" board[5] = 'O'\n",
" if move == 3 and board[4] == 'X' and board[8] == 'X':\n",
" board[2] = 'O'\n",
" if move == 5 and board[4] == 'X' and board[8] == 'X' and board[1] == ' ':\n",
" board[1] = 'O'\n",
" elif move == 5 and board[4] == 'X' and board[8] == 'X' and board[1] == 'X':\n",
" board[7] = 'O'\n",
" if move == 7 and board[4] == 'X' and board[8] == 'X' and board[1] == 'X' and board[3] == 'X':\n",
" board[5] = 'O'\n",
" elif move == 7 and board[4] == 'X' and board[8] == 'X' and board[1] == 'X' and board[5] == 'X':\n",
" board[5] = 'O'\n",
" elif move == 7 and board[4] == 'X' and board[8] == 'X' and board[1] == 'X' and board[6] == 'X':\n",
" board[5] = 'O'"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# F: Jacky Cheung"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def player1_f(move, board):\n",
" if move == 0:\n",
" board[4] = 'X' \n",
" \n",
" elif move == 2 and (board[3] == 'O' or board[5] == 'O'):\n",
" board[1] = 'X'\n",
" elif move == 2 and (board[1] == 'O' or board[7] == 'O'):\n",
" board[3] = 'X'\n",
" elif move == 2 and (board[0] == 'O' or board[2] == 'O' or board[6] == 'O' or board[8] =='O'):\n",
" board[1] = 'X'\n",
" \n",
" \n",
" elif move == 4 and (board[0] == 'O' and board[1] == 'O'):\n",
" board[2] = 'X'\n",
" elif move == 4 and board[7] == \" \":\n",
" board[7] = 'X'\n",
" elif move == 4 and board[5] == ' ':\n",
" board[5] = 'X'\n",
" elif move == 4 and (board[2] == 'O' and board[0] == ' '):\n",
" board[0] = 'X'\n",
" elif move == 4 and (board[6] == 'O' and board[8] == ' '):\n",
" board[8] = 'X'\n",
" elif move == 4 and (board[0] == 'O' and board[1] == 'O'):\n",
" board[2] = 'X'\n",
" elif move == 4 and (board[1] == 'O' and board[2] == 'O'):\n",
" board[0] = 'X'\n",
" elif move == 4 and (board[7] == 'O' and board[8] == \"O\" ):\n",
" board[6] = 'X' \n",
" elif move == 4 and (board[6] == 'O' and board[7] == 'O'):\n",
" board[8] == 'X'\n",
" elif move == 4 and (board[3] == 'X' and board[4] == 'X' and board[5] == ' '):\n",
" board[5] = 'X'\n",
" \n",
" elif move == 6 and board[7] == \" \":\n",
" board[7] = 'X'\n",
" elif move == 6 and board[3] == \" \":\n",
" board[3] = 'X'\n",
" elif move == 6 and board[0] == \" \":\n",
" board[0] = \"X\"\n",
" elif move == 6 and board[6] == \" \":\n",
" board[6] = \"X\"\n",
" \n",
" elif move == 8 and board[0] == ' ':\n",
" board[0] = 'X'\n",
" elif move == 8 and board[1] == ' ':\n",
" board[1] = 'X'\n",
" elif move == 8 and board[2] == ' ':\n",
" board[2] = 'X'\n",
" elif move == 8 and board[3] == ' ':\n",
" board[3] = 'X'\n",
" elif move == 8 and board[4] == ' ':\n",
" board[4] = 'X'\n",
" elif move == 8 and board[5] == ' ':\n",
" board[5] = 'X'\n",
" elif move == 8 and board[6] == ' ':\n",
" board[6] = 'X'\n",
" elif move == 8 and board[7] == ' ':\n",
" board[7] = 'X'\n",
" elif move == 8 and board[8] == ' ':\n",
" board[8] = 'X'"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def player2_f(move, board):\n",
" if move == 1 and (board[4] == 'X'):\n",
" board[0] = 'O'\n",
" elif move == 1 and board[4] == ' ':\n",
" board[4] = 'O'\n",
" \n",
" #horizontal block\n",
" elif move == 3 and (board[0] == 'X' and board[1] == 'X'):\n",
" board[2] = 'O'\n",
" elif move == 3 and (board[1] == 'X' and board[2] == 'X'):\n",
" board[0] = 'O'\n",
" elif move == 3 and (board[0] == 'X' and board[2] == 'X'):\n",
" board[1] = 'O'\n",
" elif move == 3 and (board[3] == 'X' and board[4] == 'X'):\n",
" board[5] = 'O'\n",
" elif move == 3 and (board[4] == 'X' and board[5] == 'X'):\n",
" board[3] = 'O'\n",
" elif move == 3 and (board[6] == 'X' and board[7] == 'X'):\n",
" board[8] = 'O'\n",
" elif move == 3 and (board[7] == 'X' and board[8] == 'X'):\n",
" board[6] = 'O'\n",
" elif move == 3 and (board[6] == 'X' and board[8] == 'X'):\n",
" board[7] = 'O'\n",
" #vertical block \n",
" elif move == 3 and (board[0] == 'X' and board[3] == 'X'):\n",
" board[6] = 'O'\n",
" elif move == 3 and (board[3] == 'X' and board[6] == 'X'):\n",
" board[0] = 'O'\n",
" elif move == 3 and (board[0] == 'X' and board[6] == 'X'):\n",
" board[3] = 'O'\n",
" elif move == 3 and (board[1] == 'X' and board[4] == 'X'):\n",
" board[7] = 'O'\n",
" elif move == 3 and (board[4] == 'X' and board[7] == 'X'):\n",
" board[1] = 'O'\n",
" elif move == 3 and (board[2] == 'X' and board[5] == 'X'):\n",
" board[8] = 'O'\n",
" elif move == 3 and (board[5] == 'X' and board[8] == 'X'):\n",
" board[2] = 'O'\n",
" elif move == 3 and (board[2] == 'X' and board[8] == 'X'):\n",
" board[5] = 'O'\n",
" #diagonal block \n",
" elif move == 3 and (board[0] == 'X' and board[4] == 'X'):\n",
" board[8] = 'O'\n",
" elif move == 3 and (board[4] == 'X' and board[8] == 'X'):\n",
" board[0] = 'O'\n",
" elif move == 3 and (board[4] == 'X' and board[2] == 'X'):\n",
" board[6] = 'O'\n",
" elif move == 3 and (board[4] == 'X' and board[6] == 'X'):\n",
" board[2] = 'O'\n",
" \n",
" elif move == 3 and board[0] == ' ':\n",
" board[0] = 'O'\n",
" elif move == 3 and board[2] == ' ':\n",
" board[2] = 'O'\n",
" elif move == 3 and board[6] == ' ':\n",
" board[6] = 'O'\n",
" elif move == 3 and board[8] == ' ':\n",
" board[8] = 'O' \n",
" #horizontal block\n",
" elif move == 5 and (board[0] == 'X' and board[1] == 'X'):\n",
" board[2] = 'O'\n",
" elif move == 5 and (board[1] == 'X' and board[2] == 'X'):\n",
" board[0] = 'O'\n",
" elif move == 5 and (board[0] == 'X' and board[2] == 'X'):\n",
" board[1] = 'O'\n",
" elif move == 5 and (board[3] == 'X' and board[4] == 'X'):\n",
" board[5] = 'O'\n",
" elif move == 5 and (board[4] == 'X' and board[5] == 'X'):\n",
" board[3] = 'O'\n",
" elif move == 5 and (board[6] == 'X' and board[7] == 'X'):\n",
" board[8] = 'O'\n",
" elif move == 5 and (board[7] == 'X' and board[8] == 'X'):\n",
" board[6] = 'O'\n",
" elif move == 5 and (board[6] == 'X' and board[8] == 'X'):\n",
" board[7] = 'O'\n",
" #vertical block \n",
" elif move == 5 and (board[0] == 'X' and board[3] == 'X'):\n",
" board[6] = 'O'\n",
" elif move == 5 and (board[3] == 'X' and board[6] == 'X'):\n",
" board[0] = 'O'\n",
" elif move == 5 and (board[0] == 'X' and board[6] == 'X'):\n",
" board[3] = 'O'\n",
" elif move == 5 and (board[1] == 'X' and board[4] == 'X'):\n",
" board[7] = 'O'\n",
" elif move == 5 and (board[4] == 'X' and board[7] == 'X'):\n",
" board[1] = 'O'\n",
" elif move == 5 and (board[2] == 'X' and board[5] == 'X'):\n",
" board[8] = 'O'\n",
" elif move == 5 and (board[5] == 'X' and board[8] == 'X'):\n",
" board[2] = 'O'\n",
" elif move == 5 and (board[2] == 'X' and board[8] == 'X'):\n",
" board[5] = 'O'\n",
" #diagonal block \n",
" elif move == 5 and (board[0] == 'X' and board[4] == 'X'):\n",
" board[8] = 'O'\n",
" elif move == 5 and (board[4] == 'X' and board[8] == 'X'):\n",
" board[0] = 'O'\n",
" elif move == 5 and (board[4] == 'X' and board[2] == 'X'):\n",
" board[6] = 'O'\n",
" elif move == 5 and (board[4] == 'X' and board[6] == 'X'):\n",
" board[2] = 'O'\n",
" \n",
" elif move == 5 and board[0] == ' ':\n",
" board[0] = 'O'\n",
" elif move == 5 and board[2] == ' ':\n",
" board[2] = 'O'\n",
" elif move == 5 and board[6] == ' ':\n",
" board[6] = 'O'\n",
" elif move == 5 and board[8] == ' ':\n",
" board[8] = 'O'\n",
" \n",
" #diagonal block \n",
" elif move == 7 and (board[0] == 'X' and board[4] == 'X'):\n",
" board[8] = 'O'\n",
" elif move == 7 and (board[4] == 'X' and board[8] == 'X'):\n",
" board[0] = 'O'\n",
" elif move == 7 and (board[4] == 'X' and board[2] == 'X'):\n",
" board[6] = 'O'\n",
" elif move == 7 and (board[4] == 'X' and board[6] == 'X'):\n",
" board[2] = 'O'\n",
" #horizontal block\n",
" elif move == 7 and (board[0] == 'X' and board[1] == 'X'):\n",
" board[2] = 'O'\n",
" elif move == 7 and (board[1] == 'X' and board[2] == 'X'):\n",
" board[0] = 'O'\n",
" elif move == 7 and (board[0] == 'X' and board[2] == 'X'):\n",
" board[1] = 'O'\n",
" elif move == 7 and (board[3] == 'X' and board[4] == 'X'):\n",
" board[5] = 'O'\n",
" elif move == 7 and (board[4] == 'X' and board[5] == 'X'):\n",
" board[3] = 'O'\n",
" elif move == 7 and (board[6] == 'X' and board[7] == 'X'):\n",
" board[8] = 'O'\n",
" elif move == 7 and (board[7] == 'X' and board[8] == 'X'):\n",
" board[6] = 'O'\n",
" elif move == 7 and (board[6] == 'X' and board[8] == 'X'):\n",
" board[7] = 'O'\n",
" #vertical block \n",
" elif move == 7 and (board[0] == 'X' and board[3] == 'X'):\n",
" board[6] = 'O'\n",
" elif move == 7 and (board[3] == 'X' and board[6] == 'X'):\n",
" board[0] = 'O'\n",
" elif move == 7 and (board[0] == 'X' and board[6] == 'X'):\n",
" board[3] = 'O'\n",
" elif move == 7 and (board[1] == 'X' and board[4] == 'X'):\n",
" board[7] = 'O'\n",
" elif move == 7 and (board[4] == 'X' and board[7] == 'X'):\n",
" board[1] = 'O'\n",
" elif move == 7 and (board[2] == 'X' and board[5] == 'X'):\n",
" board[8] = 'O'\n",
" elif move == 7 and (board[5] == 'X' and board[8] == 'X'):\n",
" board[2] = 'O'\n",
" elif move == 7 and (board[2] == 'X' and board[8] == 'X'):\n",
" board[5] = 'O'\n",
" \n",
" elif move == 7 and board[0] == ' ':\n",
" board[0] = 'O'\n",
" elif move == 7 and board[2] == ' ':\n",
" board[2] = 'O'\n",
" elif move == 7 and board[6] == ' ':\n",
" board[6] = 'O'\n",
" elif move == 7 and board[8] == ' ':\n",
" board[8] = 'O'"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# G: Kasper K., Abhinand J."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def player1_g(move, board):\n",
" if move == 0:\n",
" board[4] = 'X'\n",
" \n",
" elif move == 2:\n",
" board[randcornerpos(board)] = 'X'\n",
" \n",
" elif move == 4:\n",
" #position 0 is open and other player about to win in that position\n",
" if board[1] == 'O' and board[2] == 'O' and board[0] == ' ':\n",
" board[0] = 'X'\n",
" elif board[3] == 'O' and board[6] == 'O' and board[0] == ' ':\n",
" board[0] = 'X'\n",
" elif board[4] == 'O' and board[8] == 'O' and board[0] == ' ': \n",
" board[0] = 'X'\n",
" #position 1 is open and other player about to win in that position\n",
" elif board[0] == 'O' and board[2] == 'O' and board[1] == ' ':\n",
" board[1] = 'X'\n",
" elif board[4] == 'O' and board[7] == 'O' and board[1] == ' ':\n",
" board[1] = 'X'\n",
" #position 2 is open and other player about to win in that position \n",
" elif board[0] == 'O' and board[1] == 'O' and board[2] == ' ':\n",
" board[2] = 'X'\n",
" elif board[5] == 'O' and board[8] == 'O' and board[2] == ' ':\n",
" board[2] = 'X'\n",
" elif board[6] == 'O' and board [4] == 'O' and board[2] == ' ':\n",
" board[2] = 'X'\n",
" #position 3 is open and other player about to win in that position \n",
" elif board[4] == 'O' and board[5] == 'O' and board[3] == ' ':\n",
" board[3] = 'X'\n",
" elif board[0] == 'O' and board[6] == 'O' and board[3] == ' ':\n",
" board[3] = 'X'\n",
" #position 4 is open and other player about to win in that position \n",
" elif board[3] == 'O' and board[5] == 'O' and board[4] == ' ':\n",
" board[4] = 'X'\n",
" elif board[1] == 'O' and board[7] == 'O' and board[4] == ' ':\n",
" board[4] = 'X'\n",
" elif board[0] == 'O' and board[8] == 'O' and board[4] == ' ':\n",
" board[4] = 'X'\n",
" elif board[2] == 'O' and board[6] == 'O' and board[4] == ' ':\n",
" board[4] == 'X'\n",
" #position 5 is open and other player about to win in that position \n",
" elif board[3] == 'O' and board[4] == 'O' and board[5] == ' ':\n",
" board[5] = 'X'\n",
" elif board[2] == 'O' and board[8] == 'O' and board[5] == ' ':\n",
" board[5] = 'X'\n",
" #position 6 is open and other player about to win in that position \n",
" elif board[0] == 'O' and board[3] == 'O' and board[6] == ' ':\n",
" board[2] = 'X'\n",
" elif board[7] == 'O' and board[8] == 'O' and board[6] == ' ':\n",
" board[2] = 'X'\n",
" elif board[1] == 'O' and board [4] == 'O' and board[6] == ' ':\n",
" board[6] = 'X'\n",
" #position 7 is open and other player about to win in that position \n",
" elif board[6] == 'O' and board[8] == 'O' and board[7] == ' ':\n",
" board[7] = 'X'\n",
" elif board[2] == 'O' and board[4] == 'O' and board[7] == ' ':\n",
" board[7] = 'X'\n",
" #position 8 is open and other player about to win in that position \n",
" elif board[0] == 'O' and board[1] == 'O' and board[8] == ' ':\n",
" board[8] = 'X'\n",
" elif board[5] == 'O' and board[8] == 'O' and board[8] == ' ':\n",
" board[8] = 'X'\n",
" elif board[6] == 'O' and board [4] == 'O' and board[8] == ' ':\n",
" board[8] = 'X'\n",
" #other player not about to win\n",
" else:\n",
" board[randpos(board)] = 'X'\n",
" \n",
" elif move == 6:\n",
" #position 0 is open and other player about to win in that position\n",
" if board[1] == 'O' and board[2] == 'O' and board[0] == ' ':\n",
" board[0] = 'X'\n",
" elif board[3] == 'O' and board[6] == 'O' and board[0] == ' ':\n",
" board[0] = 'X'\n",
" elif board[4] == 'O' and board[8] == 'O' and board[0] == ' ': \n",
" board[0] = 'X'\n",
" #position 1 is open and other player about to win in that position\n",
" elif board[0] == 'O' and board[2] == 'O' and board[1] == ' ':\n",
" board[1] = 'X'\n",
" elif board[4] == 'O' and board[7] == 'O' and board[1] == ' ':\n",
" board[1] = 'X'\n",
" #position 2 is open and other player about to win in that position \n",
" elif board[0] == 'O' and board[1] == 'O' and board[2] == ' ':\n",
" board[2] = 'X'\n",
" elif board[5] == 'O' and board[8] == 'O' and board[2] == ' ':\n",
" board[2] = 'X'\n",
" elif board[6] == 'O' and board [4] == 'O' and board[2] == ' ':\n",
" board[2] = 'X'\n",
" #position 3 is open and other player about to win in that position \n",
" elif board[4] == 'O' and board[5] == 'O' and board[3] == ' ':\n",
" board[3] = 'X'\n",
" elif board[0] == 'O' and board[6] == 'O' and board[3] == ' ':\n",
" board[3] = 'X'\n",
" #position 4 is open and other player about to win in that position \n",
" elif board[3] == 'O' and board[5] == 'O' and board[4] == ' ':\n",
" board[4] = 'X'\n",
" elif board[1] == 'O' and board[7] == 'O' and board[4] == ' ':\n",
" board[4] = 'X'\n",
" elif board[0] == 'O' and board[8] == 'O' and board[4] == ' ':\n",
" board[4] = 'X'\n",
" elif board[2] == 'O' and board[6] == 'O' and board[4] == ' ':\n",
" board[4] == 'X'\n",
" #position 5 is open and other player about to win in that position \n",
" elif board[3] == 'O' and board[4] == 'O' and board[5] == ' ':\n",
" board[5] = 'X'\n",
" elif board[2] == 'O' and board[8] == 'O' and board[5] == ' ':\n",
" board[5] = 'X'\n",
" #position 6 is open and other player about to win in that position \n",
" elif board[0] == 'O' and board[3] == 'O' and board[6] == ' ':\n",
" board[2] = 'X'\n",
" elif board[7] == 'O' and board[8] == 'O' and board[6] == ' ':\n",
" board[2] = 'X'\n",
" elif board[1] == 'O' and board [4] == 'O' and board[6] == ' ':\n",
" board[6] = 'X'\n",
" #position 7 is open and other player about to win in that position \n",
" elif board[6] == 'O' and board[8] == 'O' and board[7] == ' ':\n",
" board[7] = 'X'\n",
" elif board[2] == 'O' and board[4] == 'O' and board[7] == ' ':\n",
" board[7] = 'X'\n",
" #position 8 is open and other player about to win in that position \n",
" elif board[0] == 'O' and board[1] == 'O' and board[8] == ' ':\n",
" board[8] = 'X'\n",
" elif board[5] == 'O' and board[8] == 'O' and board[8] == ' ':\n",
" board[8] = 'X'\n",
" elif board[6] == 'O' and board [4] == 'O' and board[8] == ' ':\n",
" board[8] = 'X'\n",
" #other player not about to win\n",
" else:\n",
" board[randpos(board)] = 'X'\n",
" \n",
" elif move == 8:\n",
" #position 0 is open and other player about to win in that position\n",
" if board[1] == 'O' and board[2] == 'O' and board[0] == ' ':\n",
" board[0] = 'X'\n",
" elif board[3] == 'O' and board[6] == 'O' and board[0] == ' ':\n",
" board[0] = 'X'\n",
" elif board[4] == 'O' and board[8] == 'O' and board[0] == ' ': \n",
" board[0] = 'X'\n",
" #position 1 is open and other player about to win in that position\n",
" elif board[0] == 'O' and board[2] == 'O' and board[1] == ' ':\n",
" board[1] = 'X'\n",
" elif board[4] == 'O' and board[7] == 'O' and board[1] == ' ':\n",
" board[1] = 'X'\n",
" #position 2 is open and other player about to win in that position \n",
" elif board[0] == 'O' and board[1] == 'O' and board[2] == ' ':\n",
" board[2] = 'X'\n",
" elif board[5] == 'O' and board[8] == 'O' and board[2] == ' ':\n",
" board[2] = 'X'\n",
" elif board[6] == 'O' and board [4] == 'O' and board[2] == ' ':\n",
" board[2] = 'X'\n",
" #position 3 is open and other player about to win in that position \n",
" elif board[4] == 'O' and board[5] == 'O' and board[3] == ' ':\n",
" board[3] = 'X'\n",
" elif board[0] == 'O' and board[6] == 'O' and board[3] == ' ':\n",
" board[3] = 'X'\n",
" #position 4 is open and other player about to win in that position \n",
" elif board[3] == 'O' and board[5] == 'O' and board[4] == ' ':\n",
" board[4] = 'X'\n",
" elif board[1] == 'O' and board[7] == 'O' and board[4] == ' ':\n",
" board[4] = 'X'\n",
" elif board[0] == 'O' and board[8] == 'O' and board[4] == ' ':\n",
" board[4] = 'X'\n",
" elif board[2] == 'O' and board[6] == 'O' and board[4] == ' ':\n",
" board[4] == 'X'\n",
" #position 5 is open and other player about to win in that position \n",
" elif board[3] == 'O' and board[4] == 'O' and board[5] == ' ':\n",
" board[5] = 'X'\n",
" elif board[2] == 'O' and board[8] == 'O' and board[5] == ' ':\n",
" board[5] = 'X'\n",
" #position 6 is open and other player about to win in that position \n",
" elif board[0] == 'O' and board[3] == 'O' and board[6] == ' ':\n",
" board[2] = 'X'\n",
" elif board[7] == 'O' and board[8] == 'O' and board[6] == ' ':\n",
" board[2] = 'X'\n",
" elif board[1] == 'O' and board [4] == 'O' and board[6] == ' ':\n",
" board[6] = 'X'\n",
" #position 7 is open and other player about to win in that position \n",
" elif board[6] == 'O' and board[8] == 'O' and board[7] == ' ':\n",
" board[7] = 'X'\n",
" elif board[2] == 'O' and board[4] == 'O' and board[7] == ' ':\n",
" board[7] = 'X'\n",
" #position 8 is open and other player about to win in that position \n",
" elif board[0] == 'O' and board[1] == 'O' and board[8] == ' ':\n",
" board[8] = 'X'\n",
" elif board[5] == 'O' and board[8] == 'O' and board[8] == ' ':\n",
" board[8] = 'X'\n",
" elif board[6] == 'O' and board [4] == 'O' and board[8] == ' ':\n",
" board[8] = 'X'\n",
" #other player not about to win\n",
" else:\n",
" board[randpos(board)] = 'X'\n",
" else:\n",
" board[(randpos(board))] = 'X'"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def player2_g(move, board):\n",
" if move == 1:\n",
" board[randpos(board)] = 'O'\n",
" \n",
" elif move == 3:\n",
" #position 0 is open and other player about to win in that position\n",
" if board[1] == 'X' and board[2] == 'X' and board[0] == ' ':\n",
" board[0] = 'O'\n",
" elif board[3] == 'X' and board[6] == 'X' and board[0] == ' ':\n",
" board[0] = 'O'\n",
" elif board[4] == 'X' and board[8] == 'X' and board[0] == ' ': \n",
" board[0] = 'O'\n",
" #position 1 is open and other player about to win in that position\n",
" elif board[0] == 'X' and board[2] == 'X' and board[1] == ' ':\n",
" board[1] = 'O'\n",
" elif board[4] == 'X' and board[7] == 'X' and board[1] == ' ':\n",
" board[1] = 'O'\n",
" #position 2 is open and other player about to win in that position \n",
" elif board[0] == 'X' and board[1] == 'X' and board[2] == ' ':\n",
" board[2] = 'O'\n",
" elif board[5] == 'X' and board[8] == 'X' and board[2] == ' ':\n",
" board[2] = 'O'\n",
" elif board[6] == 'X' and board [4] == 'X' and board[2] == ' ':\n",
" board[2] = 'O'\n",
" #position 3 is open and other player about to win in that position \n",
" elif board[6] == 'X' and board[8] == 'X' and board[3] == ' ':\n",
" board[3] = 'O'\n",
" elif board[2] == 'X' and board[4] == 'X' and board[3] == ' ':\n",
" board[3] = 'O'\n",
" #position 4 is open and other player about to win in that position \n",
" elif board[3] == 'X' and board[5] == 'X' and board[4] == ' ':\n",
" board[4] = 'O'\n",
" elif board[1] == 'X' and board[7] == 'X' and board[4] == ' ':\n",
" board[4] = 'O'\n",
" elif board[0] == 'X' and board[8] == 'X' and board[4] == ' ':\n",
" board[4] = 'O'\n",
" elif board[2] == 'X' and board[6] == 'X' and board[4] == ' ':\n",
" board[4] = 'O'\n",
" #position 5 is open and other player about to win in that position \n",
" elif board[3] == 'X' and board[4] == 'X' and board[5] == ' ':\n",
" board[5] = 'O'\n",
" elif board[2] == 'X' and board[8] == 'X' and board[5] == ' ':\n",
" board[5] = 'O'\n",
" #position 6 is open and other player about to win in that position \n",
" elif board[0] == 'X' and board[3] == 'X' and board[6] == ' ':\n",
" board[6] = 'O'\n",
" elif board[7] == 'X' and board[8] == 'X' and board[6] == ' ':\n",
" board[6] = 'O'\n",
" elif board[1] == 'X' and board [4] == 'X' and board[6] == ' ':\n",
" board[6] = 'O'\n",
" #position 7 is open and other player about to win in that position \n",
" elif board[6] == 'X' and board[8] == 'X' and board[7] == ' ':\n",
" board[7] = 'O'\n",
" elif board[2] == 'X' and board[4] == 'X' and board[7] == ' ':\n",
" board[7] = 'O'\n",
" #position 8 is open and other player about to win in that position \n",
" elif board[0] == 'X' and board[1] == 'X' and board[8] == ' ':\n",
" board[8] = 'O'\n",
" elif board[5] == 'X' and board[8] == 'X' and board[8] == ' ':\n",
" board[8] = 'O'\n",
" elif board[6] == 'X' and board [4] == 'X' and board[8] == ' ':\n",
" board[8] = 'O'\n",
" #other player not about to win\n",
" else:\n",
" board[randpos(board)] = 'O'\n",
" \n",
" elif move == 5:\n",
" #position 0 is open and other player about to win in that position\n",
" if board[1] == 'X' and board[2] == 'X' and board[0] == ' ':\n",
" board[0] = 'O'\n",
" elif board[3] == 'X' and board[6] == 'X' and board[0] == ' ':\n",
" board[0] = 'O'\n",
" elif board[4] == 'X' and board[8] == 'X' and board[0] == ' ': \n",
" board[0] = 'O'\n",
" #position 1 is open and other player about to win in that position\n",
" elif board[0] == 'X' and board[2] == 'X' and board[1] == ' ':\n",
" board[1] = 'O'\n",
" elif board[4] == 'X' and board[7] == 'X' and board[1] == ' ':\n",
" board[1] = 'O'\n",
" #position 2 is open and other player about to win in that position \n",
" elif board[0] == 'X' and board[1] == 'X' and board[2] == ' ':\n",
" board[2] = 'O'\n",
" elif board[5] == 'X' and board[8] == 'X' and board[2] == ' ':\n",
" board[2] = 'O'\n",
" elif board[6] == 'X' and board [4] == 'X' and board[2] == ' ':\n",
" board[2] = 'O'\n",
" #position 3 is open and other player about to win in that position \n",
" elif board[6] == 'X' and board[8] == 'X' and board[3] == ' ':\n",
" board[3] = 'O'\n",
" elif board[2] == 'X' and board[4] == 'X' and board[3] == ' ':\n",
" board[3] = 'O'\n",
" #position 4 is open and other player about to win in that position \n",
" elif board[3] == 'X' and board[5] == 'X' and board[4] == ' ':\n",
" board[4] = 'O'\n",
" elif board[1] == 'X' and board[7] == 'X' and board[4] == ' ':\n",
" board[4] = 'O'\n",
" elif board[0] == 'X' and board[8] == 'X' and board[4] == ' ':\n",
" board[4] = 'O'\n",
" elif board[2] == 'X' and board[6] == 'X' and board[4] == ' ':\n",
" board[4] = 'O'\n",
" #position 5 is open and other player about to win in that position \n",
" elif board[3] == 'X' and board[4] == 'X' and board[5] == ' ':\n",
" board[5] = 'O'\n",
" elif board[2] == 'X' and board[8] == 'X' and board[5] == ' ':\n",
" board[5] = 'O'\n",
" #position 6 is open and other player about to win in that position \n",
" elif board[0] == 'X' and board[3] == 'X' and board[6] == ' ':\n",
" board[6] = 'O'\n",
" elif board[7] == 'X' and board[8] == 'X' and board[6] == ' ':\n",
" board[6] = 'O'\n",
" elif board[1] == 'X' and board [4] == 'X' and board[6] == ' ':\n",
" board[6] = 'O'\n",
" #position 7 is open and other player about to win in that position \n",
" elif board[6] == 'X' and board[8] == 'X' and board[7] == ' ':\n",
" board[7] = 'O'\n",
" elif board[2] == 'X' and board[4] == 'X' and board[7] == ' ':\n",
" board[7] = 'O'\n",
" #position 8 is open and other player about to win in that position \n",
" elif board[0] == 'X' and board[1] == 'X' and board[8] == ' ':\n",
" board[8] = 'O'\n",
" elif board[5] == 'X' and board[8] == 'X' and board[8] == ' ':\n",
" board[8] = 'O'\n",
" elif board[6] == 'X' and board [4] == 'X' and board[8] == ' ':\n",
" board[8] = 'O'\n",
" #other player not about to win\n",
" else:\n",
" board[randpos(board)] = 'O'\n",
" \n",
" elif move == 7:\n",
" #position 0 is open and other player about to win in that position\n",
" if board[1] == 'X' and board[2] == 'X' and board[0] == ' ':\n",
" board[0] = 'O'\n",
" elif board[3] == 'X' and board[6] == 'X' and board[0] == ' ':\n",
" board[0] = 'O'\n",
" elif board[4] == 'X' and board[8] == 'X' and board[0] == ' ': \n",
" board[0] = 'O'\n",
" #position 1 is open and other player about to win in that position\n",
" elif board[0] == 'X' and board[2] == 'X' and board[1] == ' ':\n",
" board[1] = 'O'\n",
" elif board[4] == 'X' and board[7] == 'X' and board[1] == ' ':\n",
" board[1] = 'O'\n",
" #position 2 is open and other player about to win in that position \n",
" elif board[0] == 'X' and board[1] == 'X' and board[2] == ' ':\n",
" board[2] = 'O'\n",
" elif board[5] == 'X' and board[8] == 'X' and board[2] == ' ':\n",
" board[2] = 'O'\n",
" elif board[6] == 'X' and board [4] == 'X' and board[2] == ' ':\n",
" board[2] = 'O'\n",
" #position 3 is open and other player about to win in that position \n",
" elif board[6] == 'X' and board[8] == 'X' and board[3] == ' ':\n",
" board[3] = 'O'\n",
" elif board[2] == 'X' and board[4] == 'X' and board[3] == ' ':\n",
" board[3] = 'O'\n",
" #position 4 is open and other player about to win in that position \n",
" elif board[3] == 'X' and board[5] == 'X' and board[4] == ' ':\n",
" board[4] = 'O'\n",
" elif board[1] == 'X' and board[7] == 'X' and board[4] == ' ':\n",
" board[4] = 'O'\n",
" elif board[0] == 'X' and board[8] == 'X' and board[4] == ' ':\n",
" board[4] = 'O'\n",
" elif board[2] == 'X' and board[6] == 'X' and board[4] == ' ':\n",
" board[4] = 'O'\n",
" #position 5 is open and other player about to win in that position \n",
" elif board[3] == 'X' and board[4] == 'X' and board[5] == ' ':\n",
" board[5] = 'O'\n",
" elif board[2] == 'X' and board[8] == 'X' and board[5] == ' ':\n",
" board[5] = 'O'\n",
" #position 6 is open and other player about to win in that position \n",
" elif board[0] == 'X' and board[3] == 'X' and board[6] == ' ':\n",
" board[6] = 'O'\n",
" elif board[7] == 'X' and board[8] == 'X' and board[6] == ' ':\n",
" board[6] = 'O'\n",
" elif board[1] == 'X' and board [4] == 'X' and board[6] == ' ':\n",
" board[6] = 'O'\n",
" #position 7 is open and other player about to win in that position \n",
" elif board[6] == 'X' and board[8] == 'X' and board[7] == ' ':\n",
" board[7] = 'O'\n",
" elif board[2] == 'X' and board[4] == 'X' and board[7] == ' ':\n",
" board[7] = 'O'\n",
" #position 8 is open and other player about to win in that position \n",
" elif board[0] == 'X' and board[1] == 'X' and board[8] == ' ':\n",
" board[8] = 'O'\n",
" elif board[5] == 'X' and board[8] == 'X' and board[8] == ' ':\n",
" board[8] = 'O'\n",
" elif board[6] == 'X' and board [4] == 'X' and board[8] == ' ':\n",
" board[8] = 'O'\n",
" #other player not about to win\n",
" else:\n",
" board[randpos(board)] = 'O'\n",
" \n",
" elif move == 9:\n",
" #position 0 is open and other player about to win in that position\n",
" if board[1] == 'X' and board[2] == 'X' and board[0] == ' ':\n",
" board[0] = 'O'\n",
" elif board[3] == 'X' and board[6] == 'X' and board[0] == ' ':\n",
" board[0] = 'O'\n",
" elif board[4] == 'X' and board[8] == 'X' and board[0] == ' ': \n",
" board[0] = 'O'\n",
" #position 1 is open and other player about to win in that position\n",
" elif board[0] == 'X' and board[2] == 'X' and board[1] == ' ':\n",
" board[1] = 'O'\n",
" elif board[4] == 'X' and board[7] == 'X' and board[1] == ' ':\n",
" board[1] = 'O'\n",
" #position 2 is open and other player about to win in that position \n",
" elif board[0] == 'X' and board[1] == 'X' and board[2] == ' ':\n",
" board[2] = 'O'\n",
" elif board[5] == 'X' and board[8] == 'X' and board[2] == ' ':\n",
" board[2] = 'O'\n",
" elif board[6] == 'X' and board [4] == 'X' and board[2] == ' ':\n",
" board[2] = 'O'\n",
" #position 3 is open and other player about to win in that position \n",
" elif board[6] == 'X' and board[8] == 'X' and board[3] == ' ':\n",
" board[3] = 'O'\n",
" elif board[2] == 'X' and board[4] == 'X' and board[3] == ' ':\n",
" board[3] = 'O'\n",
" #position 4 is open and other player about to win in that position \n",
" elif board[3] == 'X' and board[5] == 'X' and board[4] == ' ':\n",
" board[4] = 'O'\n",
" elif board[1] == 'X' and board[7] == 'X' and board[4] == ' ':\n",
" board[4] = 'O'\n",
" elif board[0] == 'X' and board[8] == 'X' and board[4] == ' ':\n",
" board[4] = 'O'\n",
" elif board[2] == 'X' and board[6] == 'X' and board[4] == ' ':\n",
" board[4] = 'O'\n",
" #position 5 is open and other player about to win in that position \n",
" elif board[3] == 'X' and board[4] == 'X' and board[5] == ' ':\n",
" board[5] = 'O'\n",
" elif board[2] == 'X' and board[8] == 'X' and board[5] == ' ':\n",
" board[5] = 'O'\n",
" #position 6 is open and other player about to win in that position \n",
" elif board[0] == 'X' and board[3] == 'X' and board[6] == ' ':\n",
" board[6] = 'O'\n",
" elif board[7] == 'X' and board[8] == 'X' and board[6] == ' ':\n",
" board[6] = 'O'\n",
" elif board[1] == 'X' and board [4] == 'X' and board[6] == ' ':\n",
" board[6] = 'O'\n",
" #position 7 is open and other player about to win in that position \n",
" elif board[6] == 'X' and board[8] == 'X' and board[7] == ' ':\n",
" board[7] = 'O'\n",
" elif board[2] == 'X' and board[4] == 'X' and board[7] == ' ':\n",
" board[7] = 'O'\n",
" #position 8 is open and other player about to win in that position \n",
" elif board[0] == 'X' and board[1] == 'X' and board[8] == ' ':\n",
" board[8] = 'O'\n",
" elif board[5] == 'X' and board[8] == 'X' and board[8] == ' ':\n",
" board[8] = 'O'\n",
" elif board[6] == 'X' and board [4] == 'X' and board[8] == ' ':\n",
" board[8] = 'O'\n",
" #other player not about to win\n",
" else:\n",
" board[randpos(board)] = 'O'\n",
" else:\n",
" board[(randpos(board))] = 'O'"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# H: Jiawei huang"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def player1_h(move, board):\n",
" if move == 0:\n",
" board[4] = 'X'\n",
" elif move == 2 and board[0] == ' ':\n",
" board[2] = 'X'\n",
" elif move == 2 and board[3] == ' ':\n",
" board[3] = 'X'\n",
" elif move == 2 and board[7] == ' ':\n",
" board[7] = 'X'\n",
" elif move == 2 and board[1] == ' ':\n",
" board[1] = 'X'\n",
" \n",
" \n",
" elif move == 4 and board[4] == 'X' and board[7] == 'X'and board[1] == ' ':\n",
" board[1] = 'X'\n",
" elif move == 4 and board[4] == 'X' and board[8] == 'X'and board[0] == ' ':\n",
" board[0] = 'X'\n",
" elif move == 4 and board[4] == 'X' and board[2] == 'X'and board[6] == ' ':\n",
" board[6] = 'X'\n",
" elif move == 4 and board[4] == 'X' and board[6] == 'X'and board[2] == ' ':\n",
" board[2] = 'X'\n",
" elif move == 4 and board[4] == 'X' and board[1] == 'X'and board[7] == ' ':\n",
" board[7] = 'X'\n",
" elif move == 4 and board[4] == 'X' and board[3] == 'X'and board[5] == ' ':\n",
" board[5] = 'X' \n",
" elif move == 4 and board[4] == 'X' and board[5] == 'X'and board[3] == ' ':\n",
" board[3] = 'X'\n",
" elif move == 4 and board[4] == 'X' and board[0] == 'X'and board[8] == ' ':\n",
" board[8] = 'X'\n",
" \n",
" \n",
"\n",
" elif move == 4 and board[0] == 'O' and board[3] == 'O'and board[6] == ' ':\n",
" board[6] = 'X'\n",
" elif move == 4 and board[3] == 'O' and board[6] == 'O'and board[0] == ' ':\n",
" board[0] = 'X'\n",
" elif move == 4 and board[0] == 'O' and board[6] == 'O'and board[3] == ' ':\n",
" board[3] = 'X'\n",
" elif move == 4 and board[1] == 'O' and board[4] == 'O'and board[7] == ' ':\n",
" board[7] = 'X'\n",
" elif move == 4 and board[1] == 'O' and board[7] == 'O'and board[4] == ' ':\n",
" board[4] = 'X'\n",
" elif move == 4 and board[4] == 'O' and board[7] == 'O'and board[1] == ' ':\n",
" board[1] = 'X'\n",
" elif move == 4 and board[2] == 'O' and board[5] == 'O'and board[8] == ' ':\n",
" board[8] = 'X'\n",
" elif move == 4 and board[5] == 'O' and board[8] == 'O'and board[2] == ' ':\n",
" board[2] = 'X' \n",
" elif move == 4 and board[2] == 'O' and board[8] == 'O'and board[5] == ' ':\n",
" board[5] = 'X' \n",
" elif move == 4 and board[0] == 'O' and board[4] == 'O'and board[8] == ' ':\n",
" board[8] = 'X' \n",
" elif move == 4 and board[0] == 'O' and board[8] == 'O'and board[4] == ' ':\n",
" board[4] = 'X' \n",
" elif move == 4 and board[4] == 'O' and board[8] == 'O'and board[0] == ' ':\n",
" board[0] = 'X' \n",
" elif move == 4 and board[2] == 'O' and board[4] == 'O'and board[6] == ' ':\n",
" board[6] = 'X' \n",
" elif move == 4 and board[2] == 'O' and board[6] == 'O'and board[4] == ' ':\n",
" board[4] = 'X' \n",
" elif move == 4 and board[4] == 'O' and board[6] == 'O'and board[2] == ' ':\n",
" board[2] = 'X'\n",
" elif move == 4 and board[0] == 'O' and board[1] == 'O'and board[2] == ' ':\n",
" board[2] = 'X'\n",
" elif move == 4 and board[1] == 'O' and board[2] == 'O'and board[0] == ' ':\n",
" board[0] = 'X'\n",
" elif move == 4 and board[0] == 'O' and board[2] == 'O'and board[1] == ' ':\n",
" board[1] = 'X'\n",
" elif move == 4 and board[3] == 'O' and board[4] == 'O'and board[5] == ' ':\n",
" board[5] = 'X'\n",
" elif move == 4 and board[4] == 'O' and board[5] == 'O'and board[3] == ' ':\n",
" board[3] = 'X'\n",
" elif move == 4 and board[3] == 'O' and board[5] == 'O'and board[4] == ' ':\n",
" board[4] = 'X'\n",
" elif move == 4 and board[6] == 'O' and board[7] == 'O'and board[8] == ' ':\n",
" board[8] = 'X'\n",
" elif move == 4 and board[7] == 'O' and board[8] == 'O'and board[6] == ' ':\n",
" board[6] = 'X'\n",
" elif move == 4 and board[6] == 'O' and board[8] == 'O'and board[7] == ' ':\n",
" board[7] = 'X'\n",
" \n",
" \n",
" elif move == 4 and board[0] == ' ':\n",
" board[0] = 'X'\n",
" elif move == 4 and board[1] == ' ':\n",
" board[1] = 'X'\n",
" elif move == 4 and board[2] == ' ':\n",
" board[2] = 'X'\n",
" elif move == 4 and board[3] == ' ':\n",
" board[3] = 'X'\n",
" elif move == 4 and board[4] == ' ':\n",
" board[4] = 'X'\n",
" elif move == 4 and board[5] == ' ':\n",
" board[5] = 'X'\n",
" elif move == 4 and board[6] == ' ':\n",
" board[6] = 'X'\n",
" elif move == 4 and board[7] == ' ':\n",
" board[7] = 'X'\n",
" elif move == 4 and board[8] == ' ':\n",
" board[8] = 'X'\n",
" \n",
" \n",
" elif move == 4 and board[2]=='X' and board[6]=='O':\n",
" board[3]='X'\n",
" elif move == 4 and board[5]=='X' and board[3]=='O':\n",
" board[6]='X'\n",
" elif move == 4 and board[7]=='X' and board[1]=='O':\n",
" board[2]='X'\n",
" elif move == 4 and board[8]=='X' and board[6]=='O':\n",
" board[3]='X'\n",
" elif move == 4 and board[8]=='X' and board[2]=='O':\n",
" board[1]='X'\n",
" \n",
" elif move == 6 and board[4] == 'X' and board[1] == 'X'and board[7] == ' ':\n",
" board[7] = 'X'\n",
" elif move == 6 and board[4] == 'X' and board[3] == 'X'and board[5] == ' ':\n",
" board[5] = 'X'\n",
" elif move == 6 and board[4] == 'X' and board[7] == 'X'and board[1] == ' ':\n",
" board[1] = 'X'\n",
" elif move == 6 and board[4] == 'X' and board[5] == 'X'and board[3] == ' ':\n",
" board[3] = 'X'\n",
" elif move == 6 and board[4] == 'X' and board[0] == 'X'and board[8] == ' ':\n",
" board[8] = 'X'\n",
" elif move == 6 and board[4] == 'X' and board[8] == 'X'and board[0] == ' ':\n",
" board[0] = 'X'\n",
" elif move == 6 and board[4] == 'X' and board[2] == 'X'and board[6] == ' ':\n",
" board[6] = 'X'\n",
" elif move == 6 and board[4] == 'X' and board[6] == 'X'and board[2] == ' ':\n",
" board[2] = 'X'\n",
" \n",
" elif move == 6 and board[0] == 'O' and board[1] == 'O'and board[2] == ' ':\n",
" board[2] = 'X'\n",
" elif move == 6 and board[1] == 'O' and board[2] == 'O'and board[0] == ' ':\n",
" board[0] = 'X'\n",
" elif move == 6 and board[0] == 'O' and board[2] == 'O'and board[1] == ' ':\n",
" board[1] = 'X'\n",
" elif move == 6 and board[3] == 'O' and board[4] == 'O'and board[5] == ' ':\n",
" board[5] = 'X'\n",
" elif move == 6 and board[4] == 'O' and board[5] == 'O'and board[3] == ' ':\n",
" board[3] = 'X'\n",
" elif move == 6 and board[3] == 'O' and board[5] == 'O'and board[4] == ' ':\n",
" board[4] = 'X'\n",
" elif move == 6 and board[6] == 'O' and board[7] == 'O'and board[8] == ' ':\n",
" board[8] = 'X'\n",
" elif move == 6 and board[7] == 'O' and board[8] == 'O'and board[6] == ' ':\n",
" board[6] = 'X'\n",
" elif move == 6 and board[6] == 'O' and board[8] == 'O'and board[7] == ' ':\n",
" board[7] = 'X'\n",
" elif move == 6 and board[0] == 'O' and board[3] == 'O'and board[6] == ' ':\n",
" board[6] = 'X'\n",
" elif move == 6 and board[3] == 'O' and board[6] == 'O'and board[0] == ' ':\n",
" board[0] = 'X'\n",
" elif move == 6 and board[0] == 'O' and board[6] == 'O'and board[3] == ' ':\n",
" board[3] = 'X'\n",
" elif move == 6 and board[1] == 'O' and board[4] == 'O'and board[7] == ' ':\n",
" board[7] = 'X'\n",
" elif move == 6 and board[1] == 'O' and board[7] == 'O'and board[4] == ' ':\n",
" board[4] = 'X'\n",
" elif move == 6 and board[4] == 'O' and board[7] == 'O'and board[1] == ' ':\n",
" board[1] = 'X'\n",
" elif move == 6 and board[2] == 'O' and board[5] == 'O'and board[8] == ' ':\n",
" board[8] = 'X'\n",
" elif move == 6 and board[5] == 'O' and board[8] == 'O'and board[2] == ' ':\n",
" board[2] = 'X' \n",
" elif move == 6 and board[2] == 'O' and board[8] == 'O'and board[5] == ' ':\n",
" board[5] = 'X' \n",
" elif move == 6 and board[0] == 'O' and board[4] == 'O'and board[8] == ' ':\n",
" board[8] = 'X' \n",
" elif move == 6 and board[0] == 'O' and board[8] == 'O'and board[4] == ' ':\n",
" board[4] = 'X' \n",
" elif move == 6 and board[4] == 'O' and board[8] == 'O'and board[0] == ' ':\n",
" board[0] = 'X' \n",
" elif move == 6 and board[2] == 'O' and board[4] == 'O'and board[6] == ' ':\n",
" board[6] = 'X' \n",
" elif move == 6 and board[2] == 'O' and board[6] == 'O'and board[4] == ' ':\n",
" board[4] = 'X' \n",
" elif move == 6 and board[4] == 'O' and board[6] == 'O'and board[2] == ' ':\n",
" board[2] = 'X'\n",
" \n",
" \n",
" elif move == 6 and board[5] ==' ':\n",
" board[5]\n",
" elif move == 6 and board[1]==' ':\n",
" board[1]='X'\n",
" elif move == 6 and board[7]==' ':\n",
" board[7]='X'\n",
" elif move == 6 and board[0] == ' ':\n",
" board[0] = 'X'\n",
" elif move == 6 and board[1] == ' ':\n",
" board[1] = 'X'\n",
" elif move == 6 and board[2] == ' ':\n",
" board[2] = 'X'\n",
" elif move == 6 and board[3] == ' ':\n",
" board[3] = 'X'\n",
" elif move == 6 and board[4] == ' ':\n",
" board[4] = 'X'\n",
" elif move == 6 and board[5] == ' ':\n",
" board[5] = 'X'\n",
" elif move == 6 and board[6] == ' ':\n",
" board[6] = 'X'\n",
" elif move == 6 and board[7] == ' ':\n",
" board[7] = 'X'\n",
" elif move == 6 and board[8] == ' ':\n",
" board[8] = 'X'\n",
" \n",
" \n",
" elif move == 8 and board[0] == ' ':\n",
" board[0] = 'X'\n",
" elif move == 8 and board[1] == ' ':\n",
" board[1] = 'X'\n",
" elif move == 8 and board[2] == ' ':\n",
" board[2] = 'X'\n",
" elif move == 8 and board[3] == ' ':\n",
" board[3] = 'X'\n",
" elif move == 8 and board[4] == ' ':\n",
" board[4] = 'X'\n",
" elif move == 8 and board[5] == ' ':\n",
" board[5] = 'X'\n",
" elif move == 8 and board[6] == ' ':\n",
" board[6] = 'X'\n",
" elif move == 8 and board[7] == ' ':\n",
" board[7] = 'X'\n",
" elif move == 8 and board[8] == ' ':\n",
" board[8] = 'X'"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def player2_h(move, board):\n",
" if move == 1 and board[4] != 'X':\n",
" board[4] = 'O'\n",
" elif move == 1 and board[4] == 'X':\n",
" board[0] = 'O'\n",
" \n",
" #only if O is in the center\n",
" elif move == 3 and board[0] == 'X' and board[6] == 'X':\n",
" board[3] = 'O'\n",
" elif move == 3 and board[6] == 'X' and board[8] == 'X':\n",
" board[7] = 'O'\n",
" elif move == 3 and board[0] == 'X' and board[2] == 'X':\n",
" board[1] = 'O'\n",
" elif move == 3 and board[2] == 'X' and board[8] == 'X':\n",
" board[5] = 'O'\n",
" elif move == 3 and board[2] == 'X' and board[6] == 'X':\n",
" board[3] = 'O'\n",
" elif move == 3 and board[0] == 'X' and board[3] == 'X':\n",
" board[6] = 'O'\n",
" elif move == 3 and board[3] == 'X' and board[6] == 'X':\n",
" board[0] = 'O'\n",
" elif move == 3 and board[6]== 'X' and board[7] == 'X':\n",
" board[8] = 'O'\n",
" elif move == 3 and board[7] == 'X' and board[8] == 'X':\n",
" board[6] = 'O'\n",
" elif move == 3 and board[8] == 'X' and board[5] == 'X':\n",
" board[2] = 'O'\n",
" elif move == 3 and board[5] == 'X' and board[2] == 'X':\n",
" board[8] = 'O'\n",
" elif move == 3 and board[1] == 'X' and board[2] == 'X':\n",
" board[0] = 'O'\n",
" elif move == 3 and board[0] == 'X' and board[1] == 'X':\n",
" board[2] = 'O'\n",
" elif move == 3 and board[0] == 'X' and board[8] == 'X':\n",
" board[3] = 'O'\n",
" elif move == 3 and board[3] == 'X' and board[7] == 'X':\n",
" board[6] = 'O'\n",
" elif move == 3 and board[3] == 'X' and board[1] == 'X':\n",
" board[0] = 'O'\n",
" elif move == 3 and board[3] == 'X' and board[5] == 'X':\n",
" board[6] = 'O'\n",
" elif move == 3 and board[1] == 'X' and board[7] == 'X':\n",
" board[0] = 'O'\n",
" elif move == 3 and board[1] == 'X' and board[5] == 'X':\n",
" board[2] = 'O'\n",
"\n",
" #only if O is not in the center\n",
" elif move == 3 and board[4] == 'X' and board[8] == 'X':\n",
" board[6] = 'O'\n",
" elif move == 3 and board[4] == 'X' and board[6] == 'X':\n",
" board[2] = 'O'\n",
" elif move == 3 and board[4] == 'X' and board[1] == 'X':\n",
" board[7] = 'O'\n",
" elif move == 3 and board[4] == 'X' and board[2] == 'X':\n",
" board[6] = 'O'\n",
" elif move == 3 and board[4] == 'X' and board[3] == 'X':\n",
" board[5] = 'O'\n",
" elif move == 3 and board[4] == 'X' and board[5] == 'X':\n",
" board[3] = 'O'\n",
" elif move == 3 and board[4] == 'X' and board[7] == 'X':\n",
" board[1] = 'O'\n",
"\n",
" #move 5 and 7 if O is in the center\n",
" \n",
" elif move == 5 and board[1] == 'X' and board[7] == 'X' and board[8] == '':\n",
" board[8] = 'O'\n",
" elif move == 5 and board[1] == 'X' and board[7] == 'X' and board[8] == 'X':\n",
" board[6] = 'O'\n",
" elif move == 5 and board[1] == 'X' and board[7] == 'X' and board[8] == 'X' and board[2] == '':\n",
" board[2] = 'O'\n",
" elif move == 5 and board[1] == 'X' and board[7] == 'X' and board[8] == 'X' and board[3] == '':\n",
" board[3] = 'O'\n",
" \n",
" elif move == 5 and board[1] == 'X' and board[5] == 'X' and board[6] == '':\n",
" board[6] = 'O'\n",
" elif move == 5 and board[1] == 'X' and board[5] == 'X' and board[6] == 'X':\n",
" board[0] = 'O'\n",
" elif move == 5 and board[1] == 'X' and board[5] == 'X' and board[6] == 'X' and board[8] == '':\n",
" board[8] = 'O'\n",
" elif move == 5 and board[1] == 'X' and board[5] == 'X' and board[6] == 'X' and board[8] == 'X':\n",
" board[7] = 'O' \n",
" \n",
" elif move == 5 and board[0] == 'X' and board[6] == 'X' and board[5] == '':\n",
" board[5] = 'O'\n",
" elif move == 5 and board[0] == 'X' and board[6] == 'X' and board[5] == 'X':\n",
" board[7] = 'O'\n",
" elif move == 7 and board[0] == 'X' and board[6] == 'X' and board[5] == 'X' and board[1] == '':\n",
" board[1] = 'O'\n",
" elif move == 7 and board[0] == 'X' and board[6] == 'X' and board[5] == 'X' and board[1] == 'X':\n",
" board[2] = 'O'\n",
" \n",
" elif move == 5 and board[3] == 'X' and board[7] == 'X' and board[2] == '':\n",
" board[2] = 'O'\n",
" elif move == 5 and board[3] == 'X' and board[7] == 'X' and board[2] == 'X':\n",
" board[8] = 'O'\n",
" elif move == 7 and board[3] == 'X' and board[7] == 'X' and board[2] == 'X' and board[0] == 'X':\n",
" board[1] = 'O'\n",
" elif move == 7 and board[3] == 'X' and board[7] == 'X' and board[2] == 'X' and board[1] == 'X':\n",
" board[0] = 'O' \n",
" \n",
" elif move == 5 and board[3] == 'X' and board[1] == 'X' and board[8] == '':\n",
" board[8] = 'O'\n",
" elif move == 5 and board[3] == 'X' and board[1] == 'X' and board[8] == 'X':\n",
" board[6] = 'O'\n",
" elif move == 7 and board[3] == 'X' and board[1] == 'X' and board[8] == 'X' and board[2] == '':\n",
" board[2] = 'O'\n",
" elif move == 7 and board[3] == 'X' and board[1] == 'X' and board[8] == 'X' and board[2] == 'X':\n",
" board[5] = 'O'\n",
" \n",
" elif move == 5 and board[3] == 'X' and board[1] == 'X' and board[2] == '':\n",
" board[2] = 'O'\n",
" elif move == 5 and board[3] == 'X' and board[1] == 'X' and board[2] == 'X':\n",
" board[8] = 'O'\n",
" elif move == 7 and board[3] == 'X' and board[1] == 'X' and board[2] and board[0] == '':\n",
" board[0] = 'O'\n",
" elif move == 7 and board[3] == 'X' and board[1] == 'X' and board[2] and board[7] == '':\n",
" board[7] = 'O'\n",
" \n",
" elif move == 5 and board[6] == 'X' and board[8] == 'X' and board[1] == '':\n",
" board[1] = 'O'\n",
" elif move == 5 and board[6] == 'X' and board[8] == 'X' and board[1] == 'X':\n",
" board[3] = 'O'\n",
" elif move == 7 and board[6] == 'X' and board[8] == 'X' and board[1] == 'X' and board[5] == '':\n",
" board[5] = 'O'\n",
" elif move == 7 and board[6] == 'X' and board[8] == 'X' and board[1] == 'X' and board[5] == 'X':\n",
" board[2] = 'O'\n",
" \n",
" \n",
" elif move == 5 and board[0] == 'X' and board[2] == 'X' and board[7] == '':\n",
" board[7] = 'O'\n",
" elif move == 5 and board[0] == 'X' and board[2] == 'X' and board[7] == 'X':\n",
" board[3] = 'O'\n",
" elif move == 7 and board[0] == 'X' and board[2] == 'X' and board[7] == 'X' and board[5] == '':\n",
" board[5] = 'O'\n",
" elif move == 7 and board[0] == 'X' and board[2] == 'X' and board[7] == 'X' and board[5] == 'X':\n",
" board[8] = 'O'\n",
" \n",
" \n",
" \n",
" elif move == 5 and board[2] == 'X' and board[8] == 'X' and board[3] == '':\n",
" board[3] = 'O'\n",
" elif move == 5 and board[2] == 'X' and board[8] == 'X' and board[3] == 'X':\n",
" board[1] = 'O'\n",
" elif move == 7 and board[2] == 'X' and board[8] == 'X' and board[3] == 'X' and board[7] == '':\n",
" board[7] = 'O'\n",
" elif move == 7 and board[2] == 'X' and board[8] == 'X' and board[3] == 'X' and board[7] == 'X':\n",
" board[6] = 'O'\n",
" \n",
" \n",
" \n",
" elif move == 5 and board[2] == 'X' and board[6] == 'X' and board[5] == '':\n",
" board[5] = 'O'\n",
" elif move == 5 and board[2] == 'X' and board[6] == 'X' and board[5] == 'X':\n",
" board[8] = 'O'\n",
" elif move == 7 and board[2] == 'X' and board[6] == 'X' and board[5] == 'X' and board[0] == '':\n",
" board[0] = 'O'\n",
" elif move == 7 and board[2] == 'X' and board[6] == 'X' and board[5] == 'X' and board[0] == 'X':\n",
" board[1] = 'O'\n",
" \n",
" \n",
" elif move == 5 and board[0] == 'X' and board[3] == 'X' and board[2] == '':\n",
" board[2] = 'O'\n",
" elif move == 5 and board[0] == 'X' and board[3] == 'X' and board[2] == 'X':\n",
" board[1] = 'O'\n",
" elif move == 7 and board[0] == 'X' and board[3] == 'X' and board[2] == 'X' and board[7] == '':\n",
" board[7]= 'X'\n",
" elif move == 7 and board[0] == 'X' and board[3] == 'X' and board[2] == 'X' and board[7] == 'X':\n",
" board[randpos(board)] = 'O'\n",
" \n",
" \n",
" elif move == 5 and board[3] == 'X' and board[6] == 'X' and board[8] == '':\n",
" board[8] = 'O'\n",
" elif move == 5 and board[3] == 'X' and board[6] == 'X' and board[8] == 'X':\n",
" board[7] = 'O'\n",
" elif move == 7 and board[3] == 'X' and board[6] == 'X' and board[8] == 'X' and board[1] == '':\n",
" board[1] = 'O'\n",
" elif move == 7 and board[3] == 'X' and board[6] == 'X' and board[8] == 'X' and board[1] == 'X':\n",
" board[randpos(board)]\n",
" \n",
" \n",
" elif move == 5 and board[6] == 'X' and board[7] == 'X' and board[0] == '':\n",
" board[0] = 'O'\n",
" elif move == 5 and board[6] == 'X' and board[7] == 'X' and board[0] == 'X':\n",
" board[3] = 'O'\n",
" elif move == 7 and board[6] == 'X' and board[7] == 'X' and board[0] == 'X' and board[5] == '':\n",
" board[5] = 'O'\n",
" elif move == 7 and board[6] == 'X' and board[7] == 'X' and board[0] == 'X' and board[5] == 'X':\n",
" board[randpos(board)]\n",
" \n",
" \n",
" elif move == 5 and board[7] == 'X' and board[8] == 'X' and board[2] == '':\n",
" board[2] = 'O'\n",
" elif move == 5 and board[7] == 'X' and board[8] == 'X' and board[2] == 'X':\n",
" board[5] = 'O'\n",
" elif move == 7 and board[7] == 'X' and board[8] == 'X' and board[2] == 'X' and board[3] == '':\n",
" board[3] = 'O'\n",
" elif move == 7 and board[7] == 'X' and board[8] == 'X' and board[2] == 'X' and board[3] == 'X':\n",
" board[randpos(board)]\n",
" \n",
" \n",
" elif move == 5 and board[8] == 'X' and board[5] == 'X' and board[6] == '':\n",
" board[6] = 'O'\n",
" elif move == 5 and board[8] == 'X' and board[5] == 'X' and board[6] == 'X':\n",
" board[7] = 'O'\n",
" elif move == 7 and board[8] == 'X' and board[5] == 'X' and board[6] == 'X' and board[1] == '':\n",
" board[1] = 'O'\n",
" elif move == 7 and board[8] == 'X' and board[5] == 'X' and board[6] == 'X' and board[1] == 'X':\n",
" board[randpos(board)]\n",
" \n",
" \n",
" elif move == 5 and board[5] == 'X' and board[2] == 'X' and board[0] == '':\n",
" board[0] = 'O'\n",
" elif move == 5 and board[5] == 'X' and board[2] == 'X' and board[0] == 'X':\n",
" board[1] = 'O'\n",
" elif move == 7 and board[5] == 'X' and board[2] == 'X' and board[0] == 'X' and board[7] == '':\n",
" board[7] = 'O'\n",
" elif move == 7 and board[5] == 'X' and board[2] == 'X' and board[0] == 'X' and board[7] == 'X':\n",
" board[randpos(board)]\n",
" \n",
" \n",
" elif move == 5 and board[1] == 'X' and board[2] == 'X' and board[8] == '':\n",
" board[8] = 'O'\n",
" elif move == 5 and board[1] == 'X' and board[2] == 'X' and board[8] == 'X':\n",
" board[5] = 'O'\n",
" elif move == 7 and board[1] == 'X' and board[2] == 'X' and board[8] == 'X' and board[3] == '':\n",
" board[3] = 'O'\n",
" elif move == 7 and board[1] == 'X' and board[2] == 'X' and board[8] == 'X' and board[3] == 'X':\n",
" board[randpos(board)]\n",
" \n",
" \n",
" elif move == 5 and board[0] == 'X' and board[1] == 'X' and board[6] == '':\n",
" board[6] = 'O'\n",
" elif move == 5 and board[0] == 'X' and board[1] == 'X' and board[6] == 'X':\n",
" board[3] = 'O'\n",
" elif move == 7 and board[0] == 'X' and board[1] == 'X' and board[6] == 'X' and board[5] == '':\n",
" board[5] = 'O'\n",
" elif move == 7 and board[0] == 'X' and board[1] == 'X' and board[6] == 'X' and board[5] == 'X':\n",
" board[randpos(board)]\n",
" \n",
" elif move == 5 and board[0] == 'X' and board[8] == 'X' and board[5] == '':\n",
" board[5] = 'O'\n",
" elif move == 5 and board[0] == 'X' and board[8] == 'X' and board[5] == 'X':\n",
" board[2] = 'O'\n",
" elif move == 7 and board[0] == 'X' and board[8] == 'X' and board[5] == 'X' and board[6] == '':\n",
" board[6] = 'O'\n",
" elif move == 7 and board[0] == 'X' and board[8] == 'X' and board[5] == 'X' and board[6] == 'X':\n",
" board[7] = 'O'\n",
" \n",
" #move 5 and 7 if O not in center \n",
" elif move == 5 and board[4] == 'X' and board[8] == 'X' and board[3] == '':\n",
" board[3] = 'O'\n",
" elif move == 5 and board[4] == 'X' and board[8] == 'X' and board[3] == 'X':\n",
" board[5] = 'O'\n",
" elif move == 7 and board[4] == 'X' and board[8] == 'X' and board[3] == 'X' and board[1] == '':\n",
" board[1] = 'O'\n",
" elif move == 7 and board[4] == 'X' and board[8] == 'X' and board[3] == 'X' and board[1] == 'X':\n",
" board[7] = 'O'\n",
" \n",
" \n",
" elif move == 5 and board[4] == 'X' and board[6] == 'X' and board[1] == '':\n",
" board[1] = 'O'\n",
" elif move == 5 and board[4] == 'X' and board[6] == 'X' and board[1] == 'X':\n",
" board[7] = 'O'\n",
" elif move == 7 and board[4] == 'X' and board[6] == 'X' and board[1] == 'X' and board[3] == 'X':\n",
" board[5] = 'O'\n",
" elif move == 7 and board[4] == 'X' and board[6] == 'X' and board[1] == 'X' and board[5] == 'X':\n",
" board[3] = 'O'\n",
" \n",
" elif move == 5 and board[4] == 'X' and board[1] == 'X' and board[3] == 'X':\n",
" board[5] = 'O'\n",
" elif move == 5 and board[4] == 'X' and board[1] == 'X' and board[5] == 'X':\n",
" board[3] = 'O' \n",
" elif move == 5 and board[4] == 'X' and board[1] == 'X' and board[6] == 'X':\n",
" board[2] = 'O'\n",
" elif move == 7 and board[4] == 'X' and board[1] == 'X' and board[3] == 'X' and board[6] == 'X':\n",
" board[2] = 'O'\n",
" elif move == 7 and board[4] == 'X' and board[1] == 'X' and board[5] == 'X' and board[6] == '':\n",
" board[6] = 'O'\n",
" elif move == 7 and board[4] == 'X' and board[1] == 'X' and board[6] == 'X' and board[3] == 'X':\n",
" board[5] = 'O'\n",
" elif move == 7 and board[4] == 'X' and board[1] == 'X' and board[6] == 'X' and board[5] == 'X':\n",
" board[3] = 'O'\n",
" \n",
" \n",
" \n",
" elif move == 5 and board[4] == 'X' and board[2] == 'X' and board[3] == '':\n",
" board[3] = 'O'\n",
" elif move == 5 and board[4] == 'X' and board[2] == 'X' and board[3] == 'X':\n",
" board[5] = 'O'\n",
" elif move == 7 and board[4] == 'X' and board[2] == 'X' and board[3] == 'X' and board[1] == 'X':\n",
" board[7] = 'O'\n",
" elif move == 7 and board[4] == 'X' and board[2] == 'X' and board[3] == 'X' and board[7] == 'X':\n",
" board[1] = 'O'\n",
" \n",
" \n",
" elif move == 5 and board[4] == 'X' and board[3] == 'X' and board[1] == 'X':\n",
" board[4] = 'O'\n",
" elif move == 5 and board[4] == 'X' and board[3] == 'X' and board[4] == 'X':\n",
" board[1] = 'O'\n",
" elif move == 5 and board[4] == 'X' and board[3] == 'X' and board[3] == 'X':\n",
" board[2] = 'O'\n",
" elif move == 5 and board[4] == 'X' and board[3] == 'X' and board[2] == 'X':\n",
" board[6] = 'O'\n",
" elif move == 7 and board[4] == 'X' and board[3] == 'X' and board[3] == 'X' and board[1] == '':\n",
" board[1] = 'O'\n",
" elif move == 7 and board[4] == 'X' and board[3] == 'X' and board[3] == 'X' and board[1] == 'X':\n",
" board[7] = 'O'\n",
" elif move == 7 and board[4] == 'X' and board[3] == 'X' and board[4] == 'X' and board[2] == '':\n",
" board[2] = 'O'\n",
" elif move == 7 and board[4] == 'X' and board[3] == 'X' and board[4] == 'X' and board[2] == 'X':\n",
" board[randpos(board)]\n",
" elif move == 7 and board[4] == 'X' and board[3] == 'X' and board[2] == 'X' and board[1] == 'X':\n",
" board[7] = 'O'\n",
" elif move == 7 and board[4] == 'X' and board[3] == 'X' and board[2] == 'X' and board[7] == 'X':\n",
" board[1] = 'O'\n",
" \n",
" \n",
" elif move == 5 and board[4] == 'X' and board[5] == 'X' and board[6] == '':\n",
" board[6] = 'O'\n",
" elif move == 5 and board[4] == 'X' and board[5] == 'X' and board[6] == 'X':\n",
" board[2] = 'O'\n",
" elif move == 7 and board[4] == 'X' and board[5] == 'X' and board[6] == 'X' and board[1] == '':\n",
" board[1] = 'O'\n",
" elif move == 5 and board[4] == 'X' and board[5] == 'X' and board[6] == 'X' and board[1] == 'X':\n",
" board[7] = 'O'\n",
" \n",
" \n",
" elif move == 5 and board[4] == 'X' and board[7] == 'X' and board[2] == '':\n",
" board[2] = 'O'\n",
" elif move == 5 and board[4] == 'X' and board[7] == 'X' and board[2] == 'X':\n",
" board[6] = 'O'\n",
" elif move == 7 and board[4] == 'X' and board[7] == 'X' and board[2] == 'X' and board[3] == '':\n",
" board[3] = 'O'\n",
" elif move == 7 and board[4] == 'X' and board[7] == 'X' and board[2] == 'X' and board[3] == 'X':\n",
" board[5] = 'O'\n",
"\n",
" else:\n",
" board[randpos(board)] = 'O'"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# I: Isaiah Madsen"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def player1_i(move, board):\n",
" global openmovex\n",
" \n",
" win = XwinClose(board)\n",
" lose = OwinClose(board)\n",
" \n",
" if (win != 10):\n",
" board[win] = 'X'\n",
" elif (lose != 10):\n",
" board[lose] = 'X'\n",
" else:\n",
" if move == 0:\n",
" random.seed()\n",
" openmovex = 3\n",
" if openmovex == 1 or openmovex == 5:\n",
" if move == 0:\n",
" board[5] = 'X'\n",
" elif move == 2 and board[6] != 'O':\n",
" board[6] = 'X'\n",
" elif move == 4 and board[8] != 'O':\n",
" board[8] = 'X'\n",
" elif move == 6 and board[7] != 'O':\n",
" board[7] = 'X'\n",
" elif move == 6 and board[2] != 'O':\n",
" board[2] = 'X'\n",
" else:\n",
" board[randpos(board)] = 'X'\n",
" if openmovex == 2 or openmovex == 6:\n",
" if move == 0:\n",
" board[4] = 'X'\n",
" elif (move == 2) and (board[0] != 'O' or board[2] != 'O' or board[6] != 'O' or board[8] != 'O'):\n",
" board[randcornerpos(board)] = 'X'\n",
" elif (move == 2) and (board[0] == 'O'):\n",
" board[8]='X'\n",
" elif (move == 2) and (board[2] == 'O'):\n",
" board[6]='X'\n",
" elif (move == 2) and (board[6] == 'O'):\n",
" board[2]='X'\n",
" elif (move == 2) and (board[8] == 'O'):\n",
" board[0]='X'\n",
" elif (move == 4):\n",
" board[randcornerpos(board)]='X'\n",
" else:\n",
" board[randpos(board)]='X'\n",
" if openmovex == 3 or openmovex == 7:\n",
" if move == 0:\n",
" board[2] = 'X'\n",
" elif move == 2 and board[4] == 'O':\n",
" board[6] = 'X'\n",
" elif move == 2 and board[6] == 'O':\n",
" board[4] = 'X'\n",
" elif move == 2 and board[0] == 'O':\n",
" board[5] = 'X'\n",
" elif move == 2 and board[8] == 'O':\n",
" board[1] = 'X'\n",
" elif move == 4 and ((board[2] == 'X' and board[1] == 'X') or (board[2] == 'X' and board[5] == 'X')):\n",
" board[4] = 'X'\n",
" else:\n",
" board[randpos(board)]='X'\n",
" if openmovex == 4 or openmovex == 8:\n",
" if move == 0:\n",
" board[5] = 'X'\n",
" elif move == 2 and board[4] == 'O':\n",
" board[6] = 'X'\n",
" elif move == 4 and board[7] != 'O':\n",
" board[7] = 'X'\n",
" else:\n",
" board[randpos(board)]='X'\n",
" if openmovex == 9:\n",
" board[randpos(board)]='X'"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def player2_i(move, board):\n",
" global openmove, openspot\n",
" win = OwinClose(board)\n",
" lose = XwinClose(board)\n",
" #X is in the middle ---- 1\n",
" #X is in a corner ---- 2\n",
" #X is in a side piece ---- 3\n",
" \n",
" if (win != 10):\n",
" board[win]='O'\n",
" elif (lose != 10):\n",
" board[lose]='O'\n",
" \n",
" elif move == 1:\n",
" count = 0\n",
" for x in range(0,8):\n",
" if board[x] == 'X':\n",
" openspot = x\n",
" \n",
" if board[4] == 'X':\n",
" board[2] = 'O'\n",
" openmove = 1\n",
" \n",
" elif (board[1] == 'X' or board[3] == 'X' or board[5] == 'X' or board[7] == 'X'):\n",
" board[4] = 'O'\n",
" openmove = 3\n",
" \n",
" elif board[4] == ' ':\n",
" board[4] = 'O'\n",
" openmove = 2\n",
" \n",
" \n",
" elif move == 3:\n",
" win = OwinClose(board)\n",
" if (lose != 10):\n",
" board[lose] = 'O'\n",
" \n",
" \n",
" elif openmove == 1:\n",
" if (board[6] == 'X'):\n",
" board[randcornerpos(board)] = 'O'\n",
" else: \n",
" board[randpos(board)]\n",
" elif openmove == 2:\n",
" if openspot == 0 or openspot == 6:\n",
" board[3] = 'O'\n",
" if openspot == 2 or openspot == 8:\n",
" board[5] = 'O'\n",
" \n",
" \n",
" elif openmove == 3:\n",
" if (board[1] == 'X' and board[5] == 'X') and board[2] != 'O':\n",
" board[2] = 'O'\n",
" elif (board[1] == 'X' and board[3] == 'X') and board[0] != 'O':\n",
" board[0] = 'O'\n",
" elif (board[7] == 'X' and board[5] == 'X') and board[8] != 'O':\n",
" board[8] = 'O'\n",
" elif (board[7] == 'X' and board[3] == 'X') and board[6] != 'O':\n",
" board[6] = 'O'\n",
" else:\n",
" board[randpos(board)] = 'O'\n",
" \n",
" elif move == 5:\n",
" \n",
" if (win != 10):\n",
" board[win]='O'\n",
" elif (lose != 10):\n",
" board[lose]='O'\n",
" elif (board[1] == 'X' and board[5] == 'X') and board[2] != 'O':\n",
" board[2] = 'O'\n",
" elif (board[1] == 'X' and board[3] == 'X') and board[0] != 'O':\n",
" board[0] = 'O'\n",
" elif (board[7] == 'X' and board[5] == 'X') and board[8] != 'O':\n",
" board[8] = 'O'\n",
" elif (board[7] == 'X' and board[3] == 'X') and board[6] != 'O':\n",
" board[6] = 'O'\n",
" else:\n",
" board[randpos(board)] = 'O'\n",
" \n",
" elif move ==7:\n",
" board[randpos(board)]='O'"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#Checks to see if O is about to win\n",
"def OwinClose(board):\n",
" #Horizontals ends\n",
" for line in ((0,1),(3,4),(6,7)):\n",
" if (all(board[i] == 'O' for i in line)) and (board[line[1]+1] != 'X'):\n",
" return (line[1]+1)\n",
" \n",
" #Horizontals Beginnings\n",
" for line in ((1,2),(4,5),(7,8)):\n",
" if (all(board[i] == 'O' for i in line)) and (board[line[0]-1] != 'X'):\n",
" return (line[0]-1)\n",
" \n",
" #Horizontal Middles\n",
" for line in ((0,2),(3,5),(6,8)):\n",
" if (all(board[i] == 'O' for i in line)) and (board[line[1]-1] != 'X'):\n",
" return (line[1]-1) \n",
" \n",
" #Vertical bottom\n",
" for line in ((0,3),(1,4),(2,5)):\n",
" if (all(board[i] == 'O' for i in line)) and (board[line[1]+3] != 'X'):\n",
" return (line[1]+3)\n",
"\n",
" #Vertical Tops\n",
" for line in ((3,6),(4,7),(5,8)):\n",
" if (all(board[i] == 'O' for i in line)) and (board[line[0]-3] != 'X'):\n",
" return (line[0]-3)\n",
" \n",
" #Veritcal Middles\n",
" for line in ((2,8),(0,6),(1,7)):\n",
" if (all(board[i] == 'O' for i in line)) and (board[line[1]-3] != 'X'):\n",
" return (line[1]-3)\n",
" \n",
" #Diagonals Below. We used if statements for diagonals because one diagonal increases by 2 and the other 4\n",
" if (board[0] == 'O' and board[4] == 'O') and (board[8] != 'X'):\n",
" return 8\n",
" if (board[4] == 'O' and board[8] == 'O') and (board[0] != 'X'):\n",
" return 0\n",
" if (board[4] == 'O' and board[8] == 'O') and (board[0] != 'X'):\n",
" return 0\n",
" if (board[2] == 'O' and board[4] == 'O') and (board[6] != 'X'):\n",
" return 6\n",
" if (board[2] == 'O' and board[6] == 'O') and (board[4] != 'X'):\n",
" return 4\n",
" if (board[4] == 'O' and board[6] == 'O') and (board[2] != 'X'):\n",
" return 2\n",
" \n",
" return 10\n",
"\n",
"\n",
"#Checks to see if X is about to win\n",
"def XwinClose(board):\n",
" \n",
" #Horizontals ends\n",
" for line in ((0,1),(3,4),(6,7)):\n",
" if (all(board[i] == 'X' for i in line)) and (board[line[1]+1] != 'O'):\n",
" return (line[1]+1)\n",
" #Horizontals Beginnings\n",
" for line in ((1,2),(4,5),(7,8)):\n",
" if (all(board[i] == 'X' for i in line)) and (board[line[0]-1] != 'O'):\n",
" return (line[0]-1)\n",
" #Horizontal Middles\n",
" for line in ((0,2),(3,5),(6,8)):\n",
" if (all(board[i] == 'X' for i in line)) and (board[line[1]-1] != 'O'):\n",
" return (line[1]-1)\n",
" \n",
" #Vertical bottom\n",
" for line in ((0,3),(1,4),(2,5)):\n",
" if (all(board[i] == 'X' for i in line)) and (board[line[1]+3] != 'O'):\n",
" return (line[1]+3)\n",
"\n",
" #Vertical Tops\n",
" for line in ((3,6),(4,7),(5,8)):\n",
" if (all(board[i] == 'X' for i in line)) and (board[line[0]-3] != 'O'):\n",
" return (line[0]-3)\n",
" \n",
" #Veritcal Middles\n",
" for line in ((2,8),(0,6),(1,7)):\n",
" if (all(board[i] == 'X' for i in line)) and (board[line[1]-3] != 'O'):\n",
" return (line[1]-3)\n",
" \n",
" #Diagonals Below. We used if statements for diagonals because one diagonal increases by 2 and the other 4\n",
" if (board[0] == 'X' and board[4] == 'X') and (board[8] != 'O'):\n",
" return 8\n",
" elif (board[0] == 'X' and board[8] == 'X') and (board[4] != 'O'):\n",
" return 4\n",
" elif (board[4] == 'X' and board[8] == 'X') and (board[0] != 'O'):\n",
" return 0\n",
" elif (board[2] == 'X' and board[4] == 'X') and (board[6] != 'O'):\n",
" return 6\n",
" elif (board[2] == 'X' and board[6] == 'X') and (board[4] != 'O'):\n",
" return 4\n",
" elif (board[4] == 'X' and board[6] == 'X') and (board[2] != 'O'):\n",
" return 2\n",
" \n",
" return 10"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# J: Jackson Johar & Norina Balderas"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def player1_j(move, board):\n",
" #constantly checks if the middle is open \n",
" if board[4] == ' ':\n",
" board[4] = 'X'\n",
" #placement of winning X: checks if there's an empty space between two squares that already have an X in it \n",
" elif (board[0] == 'X' and board[2] == 'X') and board[1] == ' ':\n",
" board[1] = 'X' \n",
" elif (board[0] == 'X' and board[6] == 'X') and board[3] == ' ':\n",
" board[3] = 'X'\n",
" elif (board[0] == 'X' and board[8] == 'X') and board[4] == ' ':\n",
" board[4] = 'X'\n",
" elif (board[2] == 'X' and board[8] == 'X') and board[3] == ' ':\n",
" board[3] = 'X'\n",
" elif (board[2] == 'X' and board[6] == 'X') and board[4] == ' ':\n",
" board[4] = 'X'\n",
" elif (board[6] == 'X' and board[8] == 'X') and board[7] == ' ':\n",
" board[7] = 'X'\n",
" elif (board[3] == 'X' and board[5] == 'X') and board[4] == ' ':\n",
" board[4] = 'X'\n",
" elif (board[1] == 'X' and board[7] == 'X') and board[4] == ' ':\n",
" board[4] = 'X' \n",
" #placement of winning X: checks if there's an empty space on either side of two squares with an X in it\n",
" elif (board[1] == 'X' and board[2 ]== 'X' and board[0] == ' '):\n",
" board[0] = 'X'\n",
" elif (board[0] == 'X' and board[1] == 'X' and board[2] == \"\" \"\"):\n",
" board[2] = 'X'\n",
" elif (board[3] == 'X' and board[4] == 'X' and board[5] == \"\" \"\"):\n",
" board[5] = 'X'\n",
" elif (board[5] == 'X' and board[4] == 'X' and board[3] == \"\" \"\"):\n",
" board[3] = 'X'\n",
" elif (board[6] == 'X' and board[7] == 'X' and board[8] == \"\" \"\"):\n",
" board[8] = 'X'\n",
" elif (board[8] == 'X' and board[7] == 'X' and board[6] == \"\" \"\"):\n",
" board[6] = 'X'\n",
" elif (board[0] == 'X' and board[1] == 'X' and board[2] == \"\" \"\"):\n",
" board[2] = 'X'\n",
" elif (board[0] == 'X' and board[3] == 'X' and board[6] == \"\" \"\"):\n",
" board[6] = 'X'\n",
" elif (board[6] == 'X' and board[3] == 'X' and board[0] == \"\" \"\"):\n",
" board[0] = 'X'\n",
" elif (board[1] == 'X' and board[4] == 'X' and board[7] == \"\" \"\"):\n",
" board[7] = 'X'\n",
" elif (board[7] == 'X' and board[4] == 'X' and board[1] == \"\" \"\"):\n",
" board[1] = 'X'\n",
" elif (board[2] == 'X' and board[5] == 'X' and board[8] == \"\" \"\"):\n",
" board[8] = 'X'\n",
" elif (board[8] == 'X' and board[5] == 'X' and board[2] == \"\" \"\"):\n",
" board[2] = 'X'\n",
" elif (board[0] == 'X' and board[4] == 'X' and board[8] == ' '):\n",
" board[8] = 'X'\n",
" elif (board[8] == 'X' and board[4] == 'X' and board[0] == ' '):\n",
" board[0] = 'X'\n",
" elif (board[2] == 'X' and board[4] == 'X' and board[6] == ' '):\n",
" board[6] = 'X'\n",
" elif (board[6] == 'X' and board[4] == 'X' and board[2] == ' '):\n",
" board[2] = 'X'\n",
" #shitty defense lul\n",
" \n",
" elif (board[0] == 'O' and board[2] == 'O') and board[1] == ' ':\n",
" board[1] = 'X' \n",
" elif (board[0] == 'O' and board[6] == 'O') and board[3] == ' ':\n",
" board[3] = 'X'\n",
" elif (board[0] == 'O' and board[8] == 'O') and board[4] == ' ':\n",
" board[4] = 'X'\n",
" elif (board[2] == 'O' and board[8] == 'O') and board[5] == ' ':\n",
" board[5] = 'X'\n",
" elif (board[2] == 'O' and board[6] == 'O') and board[4] == ' ':\n",
" board[4] = 'X'\n",
" elif (board[6] == 'O' and board[8] == 'O') and board[7] == ' ':\n",
" board[7] = 'X'\n",
" elif (board[3] == 'O' and board[5] == 'O') and board[4] == ' ':\n",
" board[4] = 'X'\n",
" elif (board[1] == 'O' and board[7] == 'O') and board[4] == ' ':\n",
" board[4] = 'X' \n",
" #blocking of winning O: checks if there's an empty space on either side of two squares with an O in it\n",
" elif (board[1] == 'O' and board[2 ]== 'O' and board[0] == ' '):\n",
" board[0] = 'X'\n",
" elif (board[0] == 'O' and board[1] == 'X' and board[2] == \"\" \"\"):\n",
" board[2] = 'X'\n",
" elif (board[3] == 'O' and board[4] == 'O' and board[5] == \"\" \"\"):\n",
" board[5] = 'X'\n",
" elif (board[5] == 'O' and board[4] == 'O' and board[3] == \"\" \"\"):\n",
" board[3] = 'X'\n",
" elif (board[6] == 'O' and board[7] == 'O' and board[8] == \"\" \"\"):\n",
" board[8] = 'X'\n",
" elif (board[8] == 'O' and board[7] == 'O' and board[6] == \"\" \"\"):\n",
" board[6] = 'X'\n",
" elif (board[0] == 'O' and board[1] == 'O' and board[2] == \"\" \"\"):\n",
" board[2] = 'X'\n",
" elif (board[0] == 'O' and board[3] == 'O' and board[6] == \"\" \"\"):\n",
" board[6] = 'X'\n",
" elif (board[6] == 'O' and board[3] == 'O' and board[0] == \"\" \"\"):\n",
" board[0] = 'X'\n",
" elif (board[1] == 'O' and board[4] == 'O' and board[7] == \"\" \"\"):\n",
" board[7] = 'X'\n",
" elif (board[7] == 'O' and board[4] == 'O' and board[1] == \"\" \"\"):\n",
" board[1] = 'X'\n",
" elif (board[2] == 'O' and board[5] == 'O' and board[8] == \"\" \"\"):\n",
" board[8] = 'X'\n",
" elif (board[8] == 'O' and board[5] == 'O' and board[2] == \"\" \"\"):\n",
" board[2] = 'X'\n",
" elif (board[0] == 'O' and board[4] == 'O' and board[8] == ' '):\n",
" board[8] = 'X'\n",
" elif (board[8] == 'O' and board[4] == 'O' and board[0] == ' '):\n",
" board[0] = 'X'\n",
" elif (board[2] == 'O' and board[4] == 'O' and board[6] == ' '):\n",
" board[6] = 'X'\n",
" elif (board[6] == 'O' and board[4] == 'O' and board[2] == ' '):\n",
" board[2] = 'X'\n",
" \n",
" \n",
" elif board[0] == ' ':\n",
" board[0] = 'X' #if the top left corner is open, take it\n",
" elif board[2]==' ':\n",
" board[2]= 'X' #if the top right corner is open, take it\n",
" elif board[6]==' ': \n",
" board[6]='X' #if the bottom left corner is open, take it\n",
" elif board[8]==' ': \n",
" board[8]='X' #if the bottom right corner is open, take it\n",
" else :\n",
" board[randpos(board)] = 'X'"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def player2_j(move, board):\n",
" #constantly checks if the middle is open \n",
" if board[4] == ' ':\n",
" board[4] = 'O'\n",
" #placement of winning X: checks if there's an empty space between two squares that already have an X in it \n",
" elif (board[0] == 'O' and board[2] == 'O') and board[1] == ' ':\n",
" board[1] = 'O' \n",
" elif (board[0] == 'O' and board[6] == 'O') and board[3] == ' ':\n",
" board[3] = 'O'\n",
" elif (board[0] == 'O' and board[8] == 'O') and board[4] == ' ':\n",
" board[4] = 'O'\n",
" elif (board[2] == 'O' and board[8] == 'O') and board[3] == ' ':\n",
" board[3] = 'O'\n",
" elif (board[2] == 'O' and board[6] == 'O') and board[4] == ' ':\n",
" board[4] = 'O'\n",
" elif (board[6] == 'O' and board[8] == 'O') and board[7] == ' ':\n",
" board[7] = 'O'\n",
" elif (board[3] == 'O' and board[5] == 'O') and board[4] == ' ':\n",
" board[4] = 'O'\n",
" elif (board[1] == 'O' and board[7] == 'O') and board[4] == ' ':\n",
" board[4] = 'O' \n",
" #placement of winning X: checks if there's an empty space on either side of two squares with an X in it\n",
" elif (board[1] == 'O' and board[2 ]== 'O' and board[0] == ' '):\n",
" board[0] = 'O'\n",
" elif (board[0] == 'O' and board[1] == 'X' and board[2] == \"\" \"\"):\n",
" board[2] = 'O'\n",
" elif (board[3] == 'O' and board[4] == 'O' and board[5] == \"\" \"\"):\n",
" board[5] = 'O'\n",
" elif (board[5] == 'O' and board[4] == 'O' and board[3] == \"\" \"\"):\n",
" board[3] = 'O'\n",
" elif (board[6] == 'O' and board[7] == 'O' and board[8] == \"\" \"\"):\n",
" board[8] = 'O'\n",
" elif (board[8] == 'O' and board[7] == 'O' and board[6] == \"\" \"\"):\n",
" board[6] = 'O'\n",
" elif (board[0] == 'O' and board[1] == 'O' and board[2] == \"\" \"\"):\n",
" board[2] = 'O'\n",
" elif (board[0] == 'O' and board[3] == 'O' and board[6] == \"\" \"\"):\n",
" board[6] = 'O'\n",
" elif (board[6] == 'O' and board[3] == 'O' and board[0] == \"\" \"\"):\n",
" board[0] = 'O'\n",
" elif (board[1] == 'O' and board[4] == 'O' and board[7] == \"\" \"\"):\n",
" board[7] = 'O'\n",
" elif (board[7] == 'O' and board[4] == 'O' and board[1] == \"\" \"\"):\n",
" board[1] = 'O'\n",
" elif (board[2] == 'O' and board[5] == 'O' and board[8] == \"\" \"\"):\n",
" board[8] = 'O'\n",
" elif (board[8] == 'O' and board[5] == 'O' and board[2] == \"\" \"\"):\n",
" board[2] = 'O'\n",
" elif (board[0] == 'O' and board[4] == 'O' and board[8] == ' '):\n",
" board[8] = 'O'\n",
" elif (board[8] == 'O' and board[4] == 'O' and board[0] == ' '):\n",
" board[0] = 'O'\n",
" elif (board[2] == 'O' and board[4] == 'O' and board[6] == ' '):\n",
" board[6] = 'O'\n",
" elif (board[6] == 'O' and board[4] == 'O' and board[2] == ' '):\n",
" board[2] = 'O'\n",
" #SHitty defense lul\n",
"\n",
" elif (board[0] == 'X' and board[2] == 'X') and board[1] == ' ':\n",
" board[1] = 'O' \n",
" elif (board[0] == 'X' and board[6] == 'X') and board[3] == ' ':\n",
" board[3] = 'O'\n",
" elif (board[0] == 'X' and board[8] == 'X') and board[4] == ' ':\n",
" board[4] = 'O'\n",
" elif (board[2] == 'X' and board[8] == 'X') and board[3] == ' ':\n",
" board[3] = 'O'\n",
" elif (board[2] == 'X' and board[6] == 'X') and board[4] == ' ':\n",
" board[4] = 'O'\n",
" elif (board[6] == 'O' and board[8] == 'X') and board[7] == ' ':\n",
" board[7] = 'O'\n",
" elif (board[3] == 'O' and board[5] == 'X') and board[4] == ' ':\n",
" board[4] = 'O'\n",
" elif (board[1] == 'X' and board[7] == 'X') and board[4] == ' ':\n",
" board[4] = 'O' \n",
" #placement of winning X: checks if there's an empty space on either side of two squares with an X in it\n",
" elif (board[1] == 'X' and board[2 ]== 'X' and board[0] == ' '):\n",
" board[0] = 'O'\n",
" elif (board[0] == 'X' and board[1] == 'X' and board[2] == \"\" \"\"):\n",
" board[2] = 'O'\n",
" elif (board[3] == 'X' and board[4] == 'X' and board[5] == \"\" \"\"):\n",
" board[5] = 'O'\n",
" elif (board[5] == 'X' and board[4] == 'X' and board[3] == \"\" \"\"):\n",
" board[3] = 'O'\n",
" elif (board[6] == 'X' and board[7] == 'X' and board[8] == \"\" \"\"):\n",
" board[8] = 'O'\n",
" elif (board[8] == 'X' and board[7] == 'X' and board[6] == \"\" \"\"):\n",
" board[6] = 'O'\n",
" elif (board[0] == 'X' and board[1] == 'X' and board[2] == \"\" \"\"):\n",
" board[2] = 'O'\n",
" elif (board[0] == 'X' and board[3] == 'X' and board[6] == \"\" \"\"):\n",
" board[6] = 'O'\n",
" elif (board[6] == 'X' and board[3] == 'X' and board[0] == \"\" \"\"):\n",
" board[0] = 'O'\n",
" elif (board[1] == 'X' and board[4] == 'X' and board[7] == \"\" \"\"):\n",
" board[7] = 'O'\n",
" elif (board[7] == 'X' and board[4] == 'X' and board[1] == \"\" \"\"):\n",
" board[1] = 'O'\n",
" elif (board[2] == 'X' and board[5] == 'X' and board[8] == \"\" \"\"):\n",
" board[8] = 'O'\n",
" elif (board[8] == 'X' and board[5] == 'X' and board[2] == \"\" \"\"):\n",
" board[2] = 'O'\n",
" elif (board[0] == 'X' and board[4] == 'X' and board[8] == ' '):\n",
" board[8] = 'O'\n",
" elif (board[8] == 'X' and board[4] == 'X' and board[0] == ' '):\n",
" board[0] = 'O'\n",
" elif (board[2] == 'X' and board[4] == 'X' and board[6] == ' '):\n",
" board[6] = 'O'\n",
" elif (board[6] == 'X' and board[4] == 'X' and board[2] == ' '):\n",
" board[2] = 'O'\n",
" \n",
" \n",
" \n",
" elif board[0] == ' ':\n",
" board[0] = 'O' #if the top left corner is open, take it\n",
" elif board[2]==' ':\n",
" board[2]= 'O' #if the top right corner is open, take it\n",
" elif board[6]==' ': \n",
" board[6]='O' #if the bottom left corner is open, take it\n",
" elif board[8]==' ': \n",
" board[8]='O' #if the bottom right corner is open, take it \n",
" else :\n",
" board[randpos(board)] = 'O'"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# K: Yongsi Liu"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def player1_k(move, board):\n",
" board[randpos(board)] = 'X'\n",
" if move == 0:\n",
" board[4]== 'X'\n",
" elif move == 1 and (board[3]== 'O'or board[5] == 'O'or board[1]=='O'or board [7] =='O'or board [0] =='O'or board [6] =='O'or board [2] =='O'):\n",
" pass\n",
" board[8]== 'X'\n",
" else:\n",
" board[0]== 'X'\n",
" if move == 4:\n",
" board[2]== 'X'\n",
" elif move == 5 and (board[5]== 'O'):\n",
" pass\n",
" board[6]== 'X'\n",
" else:\n",
" board[5]== 'X'"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def player2_k(move, board):\n",
" board[randpos(board)] == 'O'\n",
" if move == 0 and board[4]== 'X':\n",
" pass\n",
" board[8]=='O'\n",
" else:\n",
" board[4]=='O'\n",
" if move == 2 and (board[3]== 'X' or board[5]== 'X'):\n",
" pass\n",
" board[6]=='O'\n",
" else:\n",
" board[0]=='O'\n",
" if move == 4 and (board[7]=='X'):\n",
" pass\n",
" board[2]=='O'\n",
" else:\n",
" board[0]=='O'\n",
" if move == 7:\n",
" board[1]='O'"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# L: Guadalupe Gonzalez"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def player1_l(move, board):\n",
" if move ==0:\n",
" board[4]= 'X'\n",
" \n",
" elif move==2 and (board[0] =='O'):\n",
" board[2]= 'X'\n",
"\n",
" \n",
" elif move==4 and (board[2] =='O'):\n",
" board[5]='X'\n",
"\n",
" \n",
" elif move==6 and (board[6]=='O'):\n",
" board[8]='X'\n",
" else:\n",
" board[6]='X'"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def player2_l(move, board):\n",
" if move==1 and (board[4]=='X'):\n",
" board[0]= 'O'\n",
" \n",
" \n",
" elif move==3 and (board[6]=='X'):\n",
" board[8]='O'\n",
" else:\n",
" board[6]='O'\n",
" \n",
" if move==5:\n",
" board[3]='O'"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# M: Zhuo Hui Deng"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def player1_m(move, board):\n",
"\n",
" if move == 0:\n",
" board[6] = 'X'\n",
"\n",
" elif move == 2 and board[4] == 'O' and board[2] == ' ':\n",
" board[2] = 'X'\n",
" elif move == 4 and board[1] == 'O' and board [7] == ' ' :\n",
" board[7] = 'X'\n",
" elif move == 4 and board[3] == 'O' and board [5] == ' ' :\n",
" board[5] = 'X'\n",
" elif move == 4 and board[5] == 'O' and board [3] == ' ' :\n",
" board[3] = 'X'\n",
" elif move == 4 and board[7] == 'O' and board [1] == ' ' :\n",
" board[1] = 'X'\n",
" elif move == 6 and board[4] == 'O' and board [0] == 'O' :\n",
" board[8] = 'X'\n",
" elif move == 6 and board[4] == 'O' and board [2] == 'O' :\n",
" board[6] = 'X'\n",
" elif move == 6 and board[4] == 'O' and board [6] == 'O' :\n",
" board[2] = 'X'\n",
" elif move == 6 and board[4] == 'O' and board [8] == 'O' :\n",
" board[0] = 'X'\n",
" else: \n",
" board[randpos(board)] = 'X'"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def player2_m(move, board):\n",
" if move == 1:\n",
" board[4] = 'O'\n",
" elif move == 3 and (board[0] == 'X' or board[2] == 'X' or board[6] == 'X' or board[0] == '8' ):\n",
" board[7] = 'O'\n",
" elif move == 3 and board[7] == ' ' :\n",
" board[7] = 'O'\n",
" elif move ==5 and (board[1] == 'X' and board[2] == 'X'):\n",
" board[0] = 'O'\n",
" elif move == 5 and board[1] == ' ' :\n",
" board[1] = 'O'\n",
" elif move == 5 and board[3] == ' ' :\n",
" board[3] = 'O'\n",
" elif move == 5 and board[5] == ' ' :\n",
" board[5] = 'O'\n",
" elif move == 7 and (board[2] == 'X' and board[8] == 'X'):\n",
" board[5] = 'O'\n",
" elif move == 7 and (board[2] == 'X' and board[0] == 'X'):\n",
" board[1] = 'O'\n",
" elif move == 7 and (board[0] == 'X' and board[6] == 'X'):\n",
" board[3] = 'O'\n",
" else:\n",
" board[randpos(board)] = 'O'"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# N: Maria Garcia and Evan Bal"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def player1_n(move, board):\n",
" \n",
" #if X and/or O is about to win, player 1 will make a counter move\n",
" if about_to_win == 'X' or 'O':\n",
" counter_move_X()\n",
" \n",
" #if neither x or o are about to win,then player 1 will try to\n",
" #place an x in a corner\n",
" \n",
" elif(board[0] or board[2] or board[6] or board[8]) == ' ': \n",
" board[randcornerpos(board)]= 'X'\n",
" \n",
" #if neither x or o are about to win and there are no available\n",
" #corners, player 1 will place an x randomly \n",
" else:\n",
" board[randpos(board)] = 'X'"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def player2_n(move, board):\n",
" \n",
" #If player one places an x in the center on their first move,\n",
" #player 2 will avoid playing peices on edges \n",
" if (move==1) and (b[4]== 'X'):\n",
" board[randcornerpos(board)] = 'O'\n",
" \n",
" elif (move == 1) and (board[4] == ' '):\n",
" board[4] = 'O'\n",
" \n",
" elif about_to_win == 'X' or 'O':\n",
" counter_move_O()\n",
" \n",
" else: \n",
" board[randpos(board)] = 'O'"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def about_to_win(board, p0,p1,p2):\n",
" for line in winList =[(0,1,2),(3,4,5),(6,7,8),(0,4,8),(2,4,6)]\n",
" if winList.count(' ') != 1:\n",
" return False\n",
" elif winList.count('X') == 2:\n",
" return 'X'\n",
" elif winList.count('O') == 2:\n",
" return 'O'\n",
"\n",
"def counter_move_X(b):\n",
" for comb in ((0,1,2), (3,4,5), (6,7,8), (0,3,6), (1,4,7), (2,5,8), (0,4,8), (6,4,2)):\n",
" piece = about_to_win(board, comb[0], comb[1], comb[2])\n",
" if piece:\n",
" print('Found winning combination:', comb)\n",
" if board[comb[0]] == ' ':\n",
" return comb[0]\n",
" elif board[comb[1]] == ' ':\n",
" return comb[1]\n",
" else:\n",
" return comb[2]\n",
"def counter_move_O(b):\n",
" for comb in ((0,1,2), (3,4,5), (6,7,8), (0,3,6), (1,4,7), (2,5,8), (0,4,8), (6,4,2)):\n",
" piece = about_to_win(board, comb[0], comb[1], comb[2])\n",
" if piece:\n",
" print('Found winning combination:', comb)\n",
" if board[comb[0]] == ' ':\n",
" return comb[0]\n",
" elif board[comb[1]] == ' ':\n",
" return comb[1]\n",
" else:\n",
" return comb[2]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# O: Nicholas Du"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def player1_o(move, board):\n",
" if move == 0:\n",
" board[0] = 'X'\n",
" elif move == 2:\n",
" board[randcornerpos(board)] = 'X'\n",
" elif board[3] == 'O' and move == 4:\n",
" board[2] = 'X'\n",
" elif board[1] == 'O' and move == 4:\n",
" board[6] = 'X'\n",
" elif move == 6 and board[4] != 'O':\n",
" board[4] = 'X'\n",
" else:\n",
" board[randpos(board)] = 'X'"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def player2_o(move, board):\n",
" if move == 1:\n",
" board[8] = 'O'\n",
" elif board[2] == 'X' and move == 3:\n",
" board[1] = 'O'\n",
" elif board[6] == 'X' and move == 3:\n",
" board[3] = 'O'\n",
" elif move == 5 and board[1] != 'O':\n",
" board[1] = 'O'\n",
" elif move == 5 and board[3] != 'O':\n",
" board[3] = 'O'\n",
" else:\n",
" board[randpos(board)] = 'O'"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# P: Benjamin Mintz, Truong Pham"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def player1_p(move, board): # always moves first\n",
"\tpos = 0\n",
"\t\n",
"\tif move == 0:\n",
"\t\tpos = randcornerpos(board)\n",
"\tif move == 2 and board[CENTER] == 'O' and opposite_corner(board) != -1:\n",
"\t\tpos = opposite_corner(board)\n",
"\telif move >= 3:\n",
"\t\ttry:\n",
"\t\t\tpos = next_free_pos(board, 'X')\n",
"\t\texcept:\n",
"\t\t\tif DEBUG: print('next_free_pos had an error, playing randomly')\n",
"\t\t\tpos = randpos(board)\n",
"\t# if all else fails, choose randomly\n",
"\tif (pos == 0 and board[0] != ' ') or pos is None: \n",
"\t\tpos = randpos(board)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def player2_p(move, board):\n",
"\tpos = None\n",
"\t# always play center for the first move\n",
"\tif board[CENTER] == ' ' and move == 1:\n",
"\t\tpos = CENTER\n",
"\telse:\n",
"\t\ttry:\n",
"\t\t\tpos = next_free_pos(board, 'O')\n",
"\t\texcept:\n",
"\t\t\tif DEBUG: print('next_free_pos had an error, playing randomly')\n",
"\t\t\tpos = randpos(board)\n",
"\tboard[pos] = 'O'"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"TOP_LEFT = 0\n",
"TOP_EDGE = 1\n",
"TOP_RIGHT = 2\n",
"LEFT_EDGE = 3\n",
"CENTER = 4\n",
"RIGHT_EDGE = 5\n",
"BOTTOM_LEFT = 6\n",
"BOTTOM_EDGE = 7\n",
"BOTTOM_RIGHT = 8\n",
"\n",
"DEBUG = False\n",
"def opposite_corner(board, player='O'):\n",
"\t\n",
"\topposite_corners = {\n",
"\t\tTOP_LEFT: BOTTOM_RIGHT,\n",
"\t\tTOP_RIGHT: BOTTOM_LEFT,\n",
"\t\tBOTTOM_LEFT: TOP_RIGHT,\n",
"\t\tBOTTOM_RIGHT: TOP_LEFT\n",
"\t}\n",
"\t\n",
"\t\n",
"\tfor pos in (TOP_LEFT, TOP_RIGHT,\n",
"\t\t\tBOTTOM_LEFT, BOTTOM_RIGHT):\n",
"\t\tif board[pos] == player:\n",
"\t\t\treturn opposite_corners[pos]\n",
"\treturn -1\n",
"\n",
"other_player = lambda player: 'O' if player == 'X' else 'X'\n",
"\n",
"def next_free_pos(board, player):\n",
"\t\n",
"\t# which line can we block the opponent in?\n",
"\tblockable_line = None\n",
"\t\n",
"\t# is there a line that we can make a second mark in?\n",
"\t# TODO name this variable better\n",
"\tavailable_line = None\n",
"\t\n",
"\tfor line in (\n",
"\t\t((0,1,2), (3,4,5), (6,7,8), (0,3,6), \n",
"\t\t(1,4,7), (2,5,8), (0,4,8), (2,4,6))\n",
"\t):\n",
"\t\tmarks = [board[pos] for pos in line]\n",
"\t\tmy_marks = marks.count(player)\n",
"\t\ttheir_marks = marks.count(other_player(player))\n",
"\t\t\n",
"\t\t# there's a spot available for us\n",
"\t\t# or we can block their mark\n",
"\t\tif can_win(board, line, player):\n",
"\t\t\treturn last_free_pos(board, line)\n",
"\t\t# only store the first blockable line, since any of them\n",
"\t\t# are equally good choices\n",
"\t\telif can_block(board, line, player) and not blockable_line:\n",
"\t\t\tblockable_line = line\n",
"\t\telif can_play_second_move(board, line, player):\n",
"\t\t\tavailable_line = line\n",
"\t\n",
"\t# by this point, we can't win, or the for loop would've been halted\n",
"\tif blockable_line is not None:\n",
"\t\treturn last_free_pos(board, blockable_line)\n",
"\telif available_line is not None: # no blocks available AND no wins available\n",
"\t\treturn second_move(board, available_line, player)\n",
"\telse:\n",
"\t\traise ValueError(\"bruh this board ain't legit\")\n",
"\n",
"def can_block(board, line, player):\n",
"\t\n",
"\tmarks = [board[pos] for pos in line]\n",
"\topponent = other_player(player)\n",
"\t\n",
"\tif DEBUG: print('can_block?', marks)\n",
"\t\n",
"\t# are there two marks in a row from the opponent?\n",
"\treturn marks.count(opponent) == 2 and marks.count(player) == 0\n",
"\n",
"def can_win(board, line, player):\n",
"\tmarks = [board[pos] for pos in line]\n",
"\tif DEBUG: print('can_win? ', marks)\n",
"\treturn marks == [player, ' ', player]\n",
"\n",
"\n",
"def can_play_second_move(board, line, player):\n",
"\tmarks = [board[pos] for pos in line]\n",
"\treturn marks.count(player) == 1 and marks.count(' ') == 2\n",
"\n",
"def second_move(board, line, player):\n",
"\tmarks = [board[pos] for pos in line]\n",
"\t\n",
"\t# > mfw no pattern matching in Python\n",
"\tif marks == [player, ' ', ' ']:\n",
"\t\treturn line[2] # play at the other end of the line\n",
"\telif marks == [' ', ' ', player]:\n",
"\t\treturn line[0]\n",
"\telif marks == [' ', player, ' ']:\n",
"\t\treturn random.choice((pos[0], pos[2]))\n",
"\t\n",
"\n",
"# TODO add \"\"can block\"\" and \"\"can win\"\" functions that take in a line\n",
"# return blocking pos iff you can't win\n",
"\n",
"def last_free_pos(board, line):\n",
"\t# first empty spot (assume the line only has one empty spot)\n",
"\tmarks = [pos for pos in line]\n",
"\tif DEBUG: print('last_free_pos called', marks)\n",
"\treturn marks.index(' ')\n",
"\n",
"\n",
"def possible_moves(board):\n",
"\treturn [pos for pos in range(9) if board[pos] == ' ']\n",
"\n",
"def update_state(board, move, pos):\n",
"\tboard = list(board) # don't modify in place\n",
"\tboard[pos] = 'X' if depth % 2 else 'O'\n",
"\treturn board"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Q: Jiawen Zhou"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def player1_q(move, board):\n",
" board[randpos(board)] = 'X'\n",
" random.seed()\n",
" player1 = random.randint(0,8)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def player2_q(move, board):\n",
" board[randpos(board)] = 'O'\n",
" random.seed()\n",
" player2 = random.randint(0,8)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# R: Ian Gersonde and Mohammed Chisti"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def player1_r(move, board):\n",
" if move == 0:\n",
" board[4] = 'X'\n",
" elif move == 2:\n",
" count = 1\n",
" for i in (0,1,2):\n",
" if board[i] == 'O':\n",
" count +=1\n",
" board[count] = 'X'\n",
" else:\n",
" board[randpos(board)] = 'X'"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def player2_r(move, board):\n",
" if move == 1:\n",
" board[randpos(board)] = 'O'\n",
" elif move == 3:\n",
" board[randpos(board)] = 'O'\n",
" else: \n",
" board[randpos(board)] = 'O'"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# S: Matt Venter, Harrison Lou"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def player1_s(move, board):\n",
" if defOrAtt(board) != -1:\n",
" board[defOrAtt(board)] = 'X'\n",
" elif move == 0:\n",
" board[randcornerpos(board)] = 'X'\n",
" elif (move == 2):\n",
" for i in (0,2,6,8):\n",
" if (board[i] == 'X'):\n",
" pos = i\n",
" for i in range(9):\n",
" if (board[i] == 'O'):\n",
" pos2 = i\n",
" if (board[4] == 'O'):\n",
" board[getOpposite(pos,board)] = 'X'\n",
" elif(pos2 == 0 or pos2 == 2 or pos2 == 6 or pos2 == 8):\n",
" board[randcornerpos(board)] = 'X'\n",
" else:\n",
" board[notBlocked(pos,board)] = 'X'\n",
"\n",
" elif (move == 4):\n",
" count = 0\n",
" for i in (0,2,6,8):\n",
" if (board[i] != ' '):\n",
" count+=1\n",
" if (count == 3):\n",
" board[randcornerpos(board)] = 'X'\n",
" else:\n",
" board[4] = 'X'\n",
" else:\n",
" board[randpos(board)] = 'X'"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def player2_s(move, board):\n",
" my_list = []\n",
" if defOrAtt(board) != -1:\n",
" board[defOrAtt(board)] = 'O'\n",
" elif (move==1):\n",
" if (board[4]!=\" \"):\n",
" board[randcornerpos(board)] = 'O'\n",
" else:\n",
" board[4]=\"O\"\n",
" elif (move==3):\n",
" for i in range(9):\n",
" if board[i]== 'X':\n",
" my_list.append(i)\n",
" if (my_list[0]==0 and my_list[1]==8):\n",
" board[1]='O'\n",
" elif (my_list[0]==2 and my_list[1]==6):\n",
" board[1]='O'\n",
" else:\n",
" board[randcornerpos(board)] = 'O' \n",
" else:\n",
" board[randpos(board)] = 'O'"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def notBlocked(pos,board):\n",
" if (pos == 0 or pos == 2):\n",
" if (board[1] != 'O'):\n",
" return 2-pos\n",
" else:\n",
" if (pos == 0):\n",
" return 6\n",
" else:\n",
" return 8\n",
" if (pos == 6 or pos == 8):\n",
" if (board[7] != 'O'):\n",
" if (pos == 6):\n",
" return 8\n",
" else:\n",
" return 6\n",
" else:\n",
" if (pos == 6):\n",
" return 0\n",
" else:\n",
" return 2\n",
"def getOpposite(pos,board):\n",
" if (pos == 0 and board[8] == ' '):\n",
" return 8\n",
" elif (pos == 8 and board[0] == ' '):\n",
" return 0\n",
" elif (pos == 2 and board[6] == ' '):\n",
" return 6\n",
" elif (pos == 6 and board[2] == ' '):\n",
" return 2\n",
" return -1\n",
"def defOrAtt(board):\n",
" for line in ((0,1,2), (3,4,5), (6,7,8), (0,3,6), \n",
" (1,4,7), (2,5,8), (0,4,8), (2,4,6)):\n",
" count = 0\n",
" temp = line\n",
" for i in line:\n",
" if (board[i] == 'X'):\n",
" count += 1\n",
" if (count == 2):\n",
" if (board[temp[0]] == ' '):\n",
" num = int(temp[0])\n",
" return num\n",
" elif (board[temp[1]] == ' '):\n",
" num1 = int(temp[1])\n",
" return num1\n",
" elif (board[temp[2]] == ' '):\n",
" num2 = int(temp[2])\n",
" return num2\n",
" for line in ((0,1,2), (3,4,5), (6,7,8), (0,3,6), \n",
" (1,4,7), (2,5,8), (0,4,8), (2,4,6)):\n",
" count1 = 0\n",
" temp = line\n",
" for i in line:\n",
" if (board[i] == 'O'):\n",
" count1 += 1\n",
" if (count1 == 2):\n",
" if (board[temp[0]] == ' '):\n",
" num = int(temp[0])\n",
" return num\n",
" elif (board[temp[1]] == ' '):\n",
" num1 = int(temp[1])\n",
" return num1\n",
" elif (board[temp[2]] == ' '):\n",
" num2 = int(temp[2])\n",
" return num2\n",
" return -1"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# T: Pawel Siglowy and Michael McCartan"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def player1_t(move, board):\n",
" if move == 0:\n",
" board[4] = 'X'\n",
" elif move == 2 or move ==4:\n",
" board[randcornerpos(board)] = 'X'\n",
" else:\n",
" board[randpos(board)] = 'X'"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def player2_t(move, board):\n",
" if move == 1 or move ==3:\n",
" board[randcornerpos(board)] = 'O'\n",
" else:\n",
" board[randpos(board)] = 'O'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Playing the game"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from IPython.display import clear_output\n",
"from time import sleep\n",
"\n",
"def playgame(player1_fn, player2_fn):\n",
" board = TTTBoard()\n",
" turn = 1\n",
" move = 0\n",
" while not board.gameover():\n",
" if turn == 1:\n",
" player1_fn(move, board)\n",
" turn = 2\n",
" else:\n",
" player2_fn(move, board)\n",
" turn = 1\n",
" move += 1\n",
" print(board)\n",
" sleep(1)\n",
" # clear_output(True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%%javascript\n",
"IPython.OutputArea.prototype._should_scroll = function(lines) {\n",
" return false; /* disable scrolling in cell output */\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"playgame(player1_i, player2_s)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"playgame(player1_s, player2_i)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Human vs. Machine!"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def playgame_interactive():\n",
" board = TTTBoard()\n",
" turn = 0\n",
" move = 0\n",
" ai_name = input('Which AI would you like to play [A-S]?').lower()\n",
" player1_fn = globals()['player1_' + ai_name]\n",
" player2_fn = globals()['player2_' + ai_name]\n",
" player_turn = int(input('Would you like to go first (1) or second (2)? '))\n",
" player_turn -= 1 \n",
" while not board.gameover():\n",
" if turn == player_turn:\n",
" player_move = int(input('Your move? '))\n",
" try:\n",
" board[player_move] = 'X' if turn == 0 else 'O'\n",
" except:\n",
" print('Bad move!')\n",
" break\n",
" elif turn == 0:\n",
" player1_fn(move, board)\n",
" else:\n",
" player2_fn(move, board)\n",
" move += 1\n",
" turn = (turn + 1) % 2\n",
" print(board)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"playgame_interactive()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}