Problem: Convert the oxo‐logic.py module to reflect OOP design by creating a Game class.  NOTE!!: This is a working code, you do not have to do anything with the code. I just need someone explain each defining function used in this python code.

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

Problem: Convert the oxo‐logic.py module to reflect OOP design by creating a Game class. 

NOTE!!: This is a working code, you do not have to do anything with the code. I just need someone explain each defining function used in this python code.  Thank you!

OXO Logic OneDrive File: https://1drv.ms/u/s!AvU1wwok8b1HauEQiCy3fFAS9ss?e=BKgPd7

return 'X'
else:
return "
def computerMove (self):
cell = self._generateMove ()
if cell == -1:
return 'D'
self.board[cell] = '0'
if self._isWinningMove () :
return 'O'
else:
return ""
def test () :
result = ""
game = Game ()
while not result:
print (game.board)
try:
result = game.userMove ( game._generateMove ())
except ValueError:
print ("Oops, that shouldn't happen")
if not result:
result = game.computerMove ()
if not result: continue
elif result == 'D':
print ("Its a draw")
else:
print ("Winner is:", result)
print (game.board)
if
name
main
":
test ()
Transcribed Image Text:return 'X' else: return " def computerMove (self): cell = self._generateMove () if cell == -1: return 'D' self.board[cell] = '0' if self._isWinningMove () : return 'O' else: return "" def test () : result = "" game = Game () while not result: print (game.board) try: result = game.userMove ( game._generateMove ()) except ValueError: print ("Oops, that shouldn't happen") if not result: result = game.computerMove () if not result: continue elif result == 'D': print ("Its a draw") else: print ("Winner is:", result) print (game.board) if name main ": test ()
import os,
import oxo_data
random
class Game () :
def _init_(self):
self.board = list ("
* 9)
def save (self, game):
save game to disk
oxo data.saveGame (self.board)
def restore (self) :
'I" restore previously saved game.
If game not restored successfully return new game
try:
self.board = oxo_data.restoreGame ()
if len (self.board) != 9:
self.board =
list (" " * 9)
return self.board
except IOError:
self.board = list (" " * 9)
return self.board
def generateMove (self):
''' generate a random cell from those available.
If all cells are used return -1'''
[i for i in range (len(self.board) ) if self.board[i] == " "]
options
if options:
return random.choice(options)
%3D
else: return -1
def _isWinningMove (self):
wins = ((0,1,2), (3,4,5), (6,7,8),
(0,3,6), (1,4,7), (2,5,8),
(0,4,8), (2,4,6))
game = self.board
for a,b,c in wins:
chars = game [a] + game [b] + game [c]
if chars == 'XXX' or chars == '000':
return True
return False
def userMove (self,cell):
if self.board[cell] != '':
raise ValueError ('Invalid cell')
else:
self.board[cell] = 'X'
if self._isWinningMove () :
Transcribed Image Text:import os, import oxo_data random class Game () : def _init_(self): self.board = list (" * 9) def save (self, game): save game to disk oxo data.saveGame (self.board) def restore (self) : 'I" restore previously saved game. If game not restored successfully return new game try: self.board = oxo_data.restoreGame () if len (self.board) != 9: self.board = list (" " * 9) return self.board except IOError: self.board = list (" " * 9) return self.board def generateMove (self): ''' generate a random cell from those available. If all cells are used return -1''' [i for i in range (len(self.board) ) if self.board[i] == " "] options if options: return random.choice(options) %3D else: return -1 def _isWinningMove (self): wins = ((0,1,2), (3,4,5), (6,7,8), (0,3,6), (1,4,7), (2,5,8), (0,4,8), (2,4,6)) game = self.board for a,b,c in wins: chars = game [a] + game [b] + game [c] if chars == 'XXX' or chars == '000': return True return False def userMove (self,cell): if self.board[cell] != '': raise ValueError ('Invalid cell') else: self.board[cell] = 'X' if self._isWinningMove () :
Expert Solution
steps

Step by step

Solved in 3 steps

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY