Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Code a Python program to play a board game with the computer. You are given a board represented as a 2- dimensional NXN square table,

image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
Code a Python program to play a board game with the computer. You are given a board represented as a 2- dimensional NXN square table, where each element of the given table is initialized with an integer data value between 100 and 800, inclusive. The aim of the game is to beat the computer, i.e., the player's accumulated points for the game should be higher than the computed points for the computer. Include the following game board definition in your program, but note that your code should work even if the board declaration below is changed to something with different values and/or a different size (but still a square board big enough to play the game): board = [[100, 250, 200, 150, 110, 320, 8001, [500, 600, 700, 450, 230, 230, 4001, [200, 225, 230, 150; 330, 145, 105), [120, 520, 500, 200, 250, 175, 205), [405, 160, 800, 350, 150, 300, 210), [125, 205, 200, 200, 110, 100, 4001, [300, 145, 120, 230, 250, 205, 6001] Your program should use the following three functions (main, printBoard and findHighest, described below) to play the game: the main function of the program should do the following: o print the given board row-wise (table as shown below in sample input/output) using the printBoard function o obtain the name of the player o obtain the number of rounds of the game to be played o generate the total points for the computer by generating a random number between 400 and 800 (inclusive) and multiplying it by the number of rounds to be played. o for each round of play . generate two random numbers, between 1 and n-2 (inclusive), one for the row index and one for the column index using the findhighest function, find the highest value from one of the 8 neighbouring cells adjacent to the randomly generated row and column index accumulate that highest value as the total points for the player display results (see sample input/output below) at the end of all the rounds of the game, print if the player won or lost the game or the game was a draw (see the sample input/output below) . . O the printBoard function should, given the board, print the board/table row-wise (one row at a time) as follows: 1100|250|200150|110320800| 1500/600/700|450|230|230|400 1200|225|230|150|330|145 1051 11201520500|200|250|175205 1405|160800|350|150|300|2101 |125 205|200200|110100400| 1300|145 120/230|2502051600 the findHighest function should, given the board and the row and column index values, find and return the highest data value in the neighbouring elements in the table (not including the value at that location itself). Note: Section 6.7.3. page 287 in the text might be helpful in navigating the neighbouring elements in the table. Sample input/output 1: 110012501200|150|11013201 8001 1500 1600 170014501230123014001 1200122512301150330|145 1051 112015201500200125011751205 1405116018001350115013002101 11251205120012001110110014001 13001145112012301250120516001 Enter player name: Henny How many rounds do you wish to play? 4 To beat the computer you must get higher than 1756 points. Your selected cell is at row 4 and column 3 Your score for this round of the game is 800 Your total points up to and including round 1 are 800 Your selected cell is at row 5 and column 2 Your score for this round of the game is 800 Your total points up to and including round 2 are 1600 Your selected cell is at row 2 and column 1 Your score for this round of the game is 700 Your total points up to and including round 3 are 2300 Your selected cell is at row 2 and column 4 Your score for this round of the game is 450 Your total points up to and including round 4 are 2750 Henny, you have accumulated total of 2750 points. Congratulations, you won the game by 994 points. I Sample input/output 2: 1100125012001150/11013201 8001 1500160017001450230 12304001 12001225123015013301145105 112015205002001250175205 140511601800135015013002101 1125120512001200111011004001 13001145112012301250120516001 Enter player name: Henny How many rounds do you wish to play? 3 To beat the computer you must get higher than 2352 points. Your selected cell is at row 5 and column 5 Your score for this round of the game is 600 Your total points up to and including round 1 are 600 Your selected cell is at row 5 and column 2 Your score for this round of the game is 800 Your total points up to and including round 2 are 1400 Your selected cell is at row 1 and columns Sample input/output 2: 11001250200|150|1101320 18001 1500 600 70014502302304001 120012251230115033011451105 11201520 1500120012501752051 140511601 80013501150130012101 1125120512001200111011001400 130011451120123012502051600 Enter player name: Henny How many rounds do you wish to play? 3 To beat the computer you must get higher than 2352 points. Your selected cell is at row 5 and column 5 Your score for this round of the game is 600 Your total points up to and including round 1 are 600 Your selected cell is at row 5 and column 2 Your score for this round of the game is 800 Your total points up to and including round 2 are 1400 Your selected cell is at row 1 and column 5 Your score for this round of the game is 800 Your total points up to and including round 3 are 2200 Henny, you have accumulated total of 2200 points. Unfortunately, you lost the game by 152 points. Good luck for the next game. Call the file containing your program boardGame.py

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Systems A Practical Approach To Design Implementation And Management

Authors: THOMAS CONNOLLY

6th Edition

9353438918, 978-9353438913

More Books

Students also viewed these Databases questions

Question

3. Review the evidence. Do you believe the testimony presented?

Answered: 1 week ago

Question

1. What are the marketing implications of this situation?

Answered: 1 week ago