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.
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...
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 ()](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F5aa516a6-089e-4743-b974-4b3db27c0b0d%2F90d02e2f-e98d-400a-becb-bab31840689c%2Fq7bpy9j_processed.png&w=3840&q=75)
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 () :](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F5aa516a6-089e-4743-b974-4b3db27c0b0d%2F90d02e2f-e98d-400a-becb-bab31840689c%2Fu0wdj5_processed.png&w=3840&q=75)
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

This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
Step by step
Solved in 3 steps

Recommended textbooks for you

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 Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science

Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning

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 Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science

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
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning

Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education

Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY