CPSC 231 - Fall XXXXXXXXXXUniversity of Calgary cpsc 231 assignment #5 Taffy Tangle If you’ve ever played casual games on your mobile device, or even on the internet through your browser, chances are...

1 answer below »
just problem 2 using python module stddraw, random, turtle, pyjama


CPSC 231 - Fall 2018 - University of Calgary cpsc 231 assignment #5 Taffy Tangle If you’ve ever played casual games on your mobile device, or even on the internet through your browser, chances are that you’ve spent some time with a “match three” game. Of the recent varieties, Candy Crush Saga, published by King in 2012 and shown in Figure 1, has been one of the most wildly successful titles for mobile platforms. Figure 1: Candy Crush Saga is an incredibly popular match-three game that has provided billions of hours of entertainment to the world. Figure 2: Bejeweled is another popular variant of the match-three game whose first version was released a full decade before Candy Crush Saga. The goal of this assignment is to create our own simple variant of a match-three game, which we’ll call “Taffy Tangle.” (Feel free to call yours whatever you’d like. We just made this one up to create a consistent alliteration through the problems of this assignment.) The base mechanics of a match-three game are quite simple. A (usually) rectangular board is initially filled with a grid of randomly- selected objects from a prescribed set. There are usually five or six varieties of these objects (be they candies, jewels, or other things) which are visually differentiated by their colour and shape. After the initial board set-up, the player makes a move by swap- ping two adjacent objects so that a line (horizontal or vertical) of at least three objects of the same kind is made. Then all lines of at least three adjacent objects of the same kind are eliminated and a score proportional to the number of objects eliminated is added to the player’s total. Remaining objects “fall” from top to bottom to fill the gaps created by the eliminated objects, and random new objects are created at the top to fill up the board again. Any new lines of three or more matches are again eliminated, and the falling and random creation process is repeated until there are no more matches. The player then continues to make moves until a certain objective (e.g. score) is met, or no valid moves remain. It’s hard to make sense of such a verbal description, so if you haven’t played Candy Crush or Bejeweled before, we encourage you to try them out. They are free to play, though be forewarned that they can be quite addictive. Don’t forget about your assignment! Due Dates Individual component Friday, November 9, 11:59 pm Paired component Friday, November 23, 11:59 pm 2 cpsc 231 - fall 2018 - university of calgary Individual Component We’re now down to a single problem for each component of the assignment! By this point in the course, we trust that you’re a well- trained computer scientist1 who doesn’t need our help anymore to 1 Or perhaps an expert computer scientist in training?apply the principles of top-down design and stepwise refinement to solving computing problems. It is entirely up to you now to exercise your skills as a computer scientist and devise algorithms to solve the problems, then plan out the structure and layout of your program, before you implement your solutions as Python programs. We believe these initial steps are best done with a pencil and a pad of paper, and we strongly recommend that you spend at least half of your time for each problem on this step. Although you’re not required to hand this in, don’t say we didn’t warn you if you decide to skip this step! You can use the exact same programming environment you set up for previous assignments to complete this assignment. Create as many modules or classes as you wish to help you organize your code, but be sure to submit all your .py files and indicate clearly which script (program file) your ta should run for each problem. Note also that the bonus problem for this assignment is attached to the individual component rather than the paired component. Problem 1: Tic-Tac-Toe Tic-Tac-Toe is another classic two-player game, played on pencil and paper, that most of us surely learned before elementary school. It is played on a three-by-three grid of squares as shown in Figure 3, and the object of the game is to claim a row of three squares on the board, either horizontally, vertically, or diagonally. Figure 3: A Tic-Tac-Toe game in progress. Figure 4: Player × has won the game by forming a line of three. The player who goes first is known as × and the second player as #. The first player starts by claiming one of the nine squares by marking it with an ×. Then the second player takes an open square by marking it with an #. The players continue to alternate turns this way until one player, the winner, makes a line of three squares in a row with his or her symbol, or until all nine squares are filled. If the grid is filled but neither player has a line of three, the game is a draw (also known as a “cat’s game”). Create a visual and interactive Python program that will facili- tate a game of Tic-Tac-Toe between two human players. Draw the board on the screen and wait for the first player to click in one fo the nine squares, then draw an × in that square. Next, allow the second player to do the same with an #. Write a message in your game’s window to indicate whose turn it is. Continue until one of the play- ers wins, or the game ends in a draw. Indicate the the final result of the game in the graphics window, then wait until the players close the window (or ask if the players would like to play another game). assignment #5: taffy tangle 3 Inputs: A series of mouse clicks from two human players taking turns to place their ×’s and #’s. Outputs: A Tic-Tac-Toe board (which looks like a #) drawn in a win- dow on your screen with ×’s and #’s drawn as they are placed. Figure 5: The game is drawn when all squares are filled and neither player has a line of three. Bonus Problem: Computer Tic-Tac-Toe Player Program a computer artificial intelligence capable of playing Tic-Tac- Toe so that you can create a single-player version of your game from Problem 1. Your ai must have at least two distinct difficulty levels: Hard should be able to play a perfect, or near-perfect, game of Tic- Tac-Toe. It should be able to force a draw against the human player every time, and take a win if the player makes a mistake. Moderate should make a mistake or sub-optimal move once in a while to give the human player a chance of winning, but still play a decent game. This difficulty ought to be great for small children. You may either use a command line argument to select the ai difficulty level or have the player click on a “button” to choose the difficulty at the beginning of the game. Simulate a coin toss to decide whether the human player will be × or #. Then play the game out interactively just as you did for Problem 1, indicating the end result in your game’s window as appropriate. Inputs: A series of mouse clicks from the human player to place his or her ×’s or #’s. Outputs: A Tic-Tac-Toe board drawn in a window on your screen with ×’s and #’s drawn as they are placed by the human and computer players. Paired Component We strongly encourage you to work with a partner to complete the remainder of this assignment. It will likely be extremely helpful to have someone with whom to talk through the strategies for solving this problem, and its sub-problems, as you do the top-down design and program decomposition for this component. Once again, your partner may be any other student in the class, but remember that you must work with a different partner for each assignment! If you are solving this problem as a pair, one submission is sufficient for both students. If you’ve already exhausted all your friends in this class through the first four assignments, perhaps you can ask your ta to help you with match-making. Maybe even try hunting for your best movie match from Assignment #3. 4 cpsc 231 - fall 2018 - university of calgary Problem 2: Taffy Tangle Your goal for this problem is to a visual and interactive Python pro- gram that allows a human player to play our variant of the match- three game, Taffy Tangle (or whatever you’ve christened your game). Our basic variant of the game will be played on a board measuring 9 rows by 7 columns with six different kinds of “taffies”, as shown in Figure 6. The player makes moves by swapping adjacent taffies so that a line (horizontal or vertical) of at least three objects of the same kind is made. The game ends when no more valid moves remain. Figure 6: The initial board of taffies in our game, with one taffy selected (in- side the red box), ready to be swapped. Figure 7: The green taffy has been swapped with the yellow one below, creating two lines of three matches. Figure 8: The two lines of matching taffies have been eliminated, leaving a gap in the middle. Your program must be able to perform the following functions to satisfy all requirements of this problem: 1. Generate an initial board (9 rows by 7 columns) filled with ran- dom selections of the 6 different kinds of taffies, and draw the board to the screen. • Each kind of taffy must be visually distinct from the others in both colour and shape. (Yours do not need to look like the ones shown as long as they meet this requirement.) • The initial board should not contain any lines (horizontal or vertical) of three or more taffies of the same kind in a row. 2. Allow the player to make moves of swapping two adjacent taffies on the board by first clicking on one taffy, then clicking on an adjacent one, while the game is not won or lost. • After the first click, indicate which taffy is “selected” in some visual way (e.g. as shown in Figure 6). • Cancel the move if the player then clicks a second taffy that is not adjacent to the first, selected one. • Forbid or undo a move that would not result in creating a new match of three or more taffies. 3. After a valid move from the player, repeatedly eliminate taffies that are matched by following the sequence of steps below, until no more matches are found. Update the visual board and pause briefly after each step so that the player can see what happened. • Eliminate all taffies on the board that are part of a horizontal or vertical line of three or more taffies of the same kind (Figure 8). • Allow taffies in each column to “fall” so that it contains a con- tinuous stack of taffies from
Answered Same DayNov 19, 2020

Answer To: CPSC 231 - Fall XXXXXXXXXXUniversity of Calgary cpsc 231 assignment #5 Taffy Tangle If you’ve ever...

Snehil answered on Nov 23 2020
145 Votes
PlayGame.py
import random;
import time;
from turtle import *
from Tokens import *
grid = []
num_rows = 9
num_cols = 7
score = 0
score_needed=100
def check_valid_moves():
for i in range (0,num_rows):
for j in range(0,num_cols):
if is_valid_position(i+1,j):
swap_grid_val([i,j],[i+1,j])
if le
n(check_combo_at(i,j))>0 or len(check_combo_at(i+1,j))>0:
swap_grid_val([i,j],[i+1,j])
return True
swap_grid_val([i,j],[i+1,j])
if is_valid_position(i,j+1):
swap_grid_val([i,j],[i,j+1])
if len(check_combo_at(i,j))>0 or len(check_combo_at(i,j+1))>0:
swap_grid_val([i,j],[i,j+1])
return True
swap_grid_val([i,j],[i,j+1])

return False
def create_grid():
for i in range(0,num_rows):
line = []
for j in range(0,num_cols):
line.append(0)
grid.append(line)
fill_empty_cells()

if check_valid_moves()==False:
print("Initial grid invalid")
grid.clear()
create_grid()

def fill_empty_cells():
for i in range(0,num_rows):
j=0
while j < num_cols:
if grid[i][j]==0:
grid[i][j] = random.randint(1,6)
if len(check_combo_at(i,j))==0:
j+=1
else:
grid[i][j]=0
else:
j+=1


def is_valid_position(i,j):
return not (i<0 or i>=num_rows or j<0 or j>=num_cols)
def color_at(i,j):
if is_valid_position(i,j):
return grid[i][j]
else:
return -1
def check_combo_at(i,j):
if color_at(i,j)<=0:
return []

combo_list=[]
m=i-1
while is_valid_position(m,j) and color_at(i,j)==color_at(m,j):
combo_list.append([m,j])
m-=1
combo_list.append([i,j])
m=i+1
while is_valid_position(m,j) and color_at(i,j)==color_at(m,j):
combo_list.append([m,j])
m+=1
if len(combo_list) >= 3:
return combo_list


combo_list=[]
n=j-1
while is_valid_position(i,n) and color_at(i,j)==color_at(i,n):
combo_list.append([i,n])
n-=1
combo_list.append([i,j])
n=j+1
while is_valid_position(i,n) and color_at(i,j)==color_at(i,n):
combo_list.append([i,n])
n+=1
if len(combo_list) >= 3:
return combo_list

return []



grid_size = 100 # pixels
window_height = grid_size * num_rows + 50
window_width = grid_size * num_cols +8
font_size = 18 # size of characters for the text

def create_drawing_canvas():

# Set up the drawing window with enough space for the grid
setup(window_width, window_height)
setworldcoordinates(0, 0, window_width, window_height)
tracer(False)
bgcolor('light grey')
home()
def draw_tokens_in_grid():
reset()
tracer(0,0)
hideturtle()
penup()
for i in range(0,num_rows):
for j in range (0,num_cols):
if grid[i][j]>0:
draw_tokens[grid[i][j]-1](j*grid_size,i*grid_size)


goto(30,num_rows*grid_size+15)
color('black')
write("Score : "+str(score), align ='left', font=('Arial', font_size, 'normal'))
update()

previous_selection = []
cur_selection = []
def set_selected(y,x):
pencolor("red")
...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here