Question

How to solve these codes with python? # Game setting constants SECTION_LENGTH = 3 ANSWER =...

How to solve these codes with python?

# Game setting constants
SECTION_LENGTH = 3
ANSWER = 'CATDOGFOXEMU'

# Move constants
SWAP = 'S'
ROTATE = 'R'
CHECK = 'C'

is_valid_move
(str) -> bool

The parameter is a string that may or may not be a valid move in the game.

This function should return True if and only if the parameter represents a valid move, i.e. it matches one of the three move constants.

is_valid_section
(int) -> bool

The parameter is an int that may or may not be a valid section number.

This function should return True if and only if the parameter represents a section number that is valid for the current answer string and section length. For example, if the answer string is 'wordlockgame' and the section length is 4, then this function should return True for the ints 1, 2, and 3, and False for all other ints.

check_section
(str, int) -> bool

The first parameter is the game state (i.e. the current state of the scrambled string), and the second parameter is a valid section number.

This function should return True if and only if the specified section in the game state matches the same section in the answer string. That is, if the specified section has been correctly unscrambled.

change_state
(str, int, str) -> str

The first parameter represents the game state, the second parameter is the section number of the section to be changed, and the third parameter is the move to be applied to the string. The move will be one of SWAP or ROTATE.

This function should return a new string that reflects the updated game state after applying the given move to the specified section. For example, if the section length is 4, and this function is called change_state ('wrdokoclgmae', 2, 'S'), then the function should return 'wrdolockgmae'.

get_move_hint
(str, int) -> str

In this function only, you may assume a fixed section length of 3.

The first parameter represents a game state, and the second parameter represents the number of a section in the game state that is not yet unscrambled.

This function should return a move (either SWAP or ROTATE) that will help the player rearrange the specified section correctly.

Use your creativity here, but make sure to give hints that if used in progression, should never produce the same game state twice. For example, always telling the user to play SWAP on a section 'ATC' each round will cause that section of the game state to go back and forth between 'ATC' and 'CTA', and never to get to the answer ('CAT').

If a player repeatedly follows your hints, they should be guaranteed to end up solving the game. Hint: Consider the cases in which SWAP is a bad move to make.

Note that this function may not work if SECTION_LENGTH is anything other than 3.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

hi, the question is not clear for change_state and get_move_hint functions. Though I implemented SWAP for change_state under the assumption only the first and last character of a section will be swapped. Let me know if this not the case. Also explain how the rotate works, clockwise or anticlockwise ?

Source & Screens: game.py
# Game setting constants
SECTION_LENGTH = 3
ANSWER = 'CATDOGFOXEMU'

# Move constants
SWAP = 'S'
ROTATE = 'R'
CHECK = 'C'


def is_valid_move(move):
    return True if move in (SWAP, ROTATE, CHECK) else False


def is_valid_section(section):
    return True if section in range(1, SECTION_LENGTH) else False


def check_section(state, section):
    start_pos = (section - 1) * SECTION_LENGTH
    end_pos = start_pos + SECTION_LENGTH
    return True if ANSWER[start_pos:end_pos] == state[start_pos:end_pos] else False


def change_state(state, section, move):
    if is_valid_move(move) and is_valid_section(section):
        state_list = list(state)
        start_pos = (section - 1) * SECTION_LENGTH
        end_pos = start_pos + SECTION_LENGTH - 1
        # if move is swap
        if move == "S":
            start_char = state_list[start_pos]
            end_char = state_list[end_pos]
            state_list[end_pos] = start_char
            state_list[start_pos] = end_char
            return "".join(state_list)
        # if move is rotate
        elif move == "R":
            c = state_list[start_pos]
            for i in range(start_pos, end_pos + 1):
                state_list[i] = state_list[i + 1]
            state_list[end_pos] = c
            return "".join(state_list)


def get_move_hint(state, section):
    if check_section(change_state(state, section, 'S'), section):
        return "SWAP"
    else:
        return "ROTATE"


print(get_move_hint("ATCGODXOFUME", 1))
print(get_move_hint("TACGODXOFUME", 1))
print(get_move_hint("CATGODXOFUME", 1))
print(check_section("CATGODXOFUME", 1))
print(change_state('wrdokoclgmae', 2, 'S'))
print(change_state('wrdokoclgmae', 2, 'R'))
Add a comment
Know the answer?
Add Answer to:
How to solve these codes with python? # Game setting constants SECTION_LENGTH = 3 ANSWER =...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • using c language String Challenge Have the function StringChallenge(str) read str which will contain two strings...

    using c language String Challenge Have the function StringChallenge(str) read str which will contain two strings separated by a space. The first string will consist of the following sets of characters: +, *, $, and {N} which is optional. The plus (+) character represents a single alphabetic character, the ($) character represents a number between 1-9, and the asterisk (*) represents a sequence of the same character of length 3 unless it is followed by {N} which represents how many...

  • C++ When running my tests for my char constructor the assertion is coming back false and...

    C++ When running my tests for my char constructor the assertion is coming back false and when printing the string garbage is printing that is different everytime but i dont know where it is wrong Requirements: You CANNOT use the C++ standard string or any other libraries for this assignment, except where specified. You must use your ADT string for the later parts of the assignment. using namespace std; is stricly forbiden. As are any global using statements. Name the...

  • #include <stdio.h> #include <stdlib.h> #include <string.h> struct game_piece { ...

    #include <stdio.h> #include <stdlib.h> #include <string.h> struct game_piece { }; struct game_board { }; void game_piece_init_default(struct game_piece* piece) { } void game_piece_init(struct game_piece* piece, char* new_label) { } char* game_piece_get_label(struct game_piece* piece) { return ""; } char* game_piece_to_string(struct game_piece* piece) { return ""; } void game_board_init(struct game_board* game_board, int rows, int cols) { } int game_board_is_space_valid(struct game_board* game_board, int row, int col) { return 0; } int game_board_add_piece(struct game_board* game_board, struct game_piece* piece, int row, int col) { return 0;...

  • using java String Challenge Have the function StringChallenge(str) read str which will contain two strings...

    Have the function wildcard(str) read str which will contain two strings separated by a space.The first string will consist of the following sets of characters: +, *, $ and {N} which is optional.The plus (+) character represents a single alphabetic character, the ($) character represents anumber between 1-9, and asterisk (*) represents a sequence of the same character of length 3unless it is followed by {N} which represents how many characters would appear in thesequence where N will be at...

  • Tic Tac Toe Game: Help, please. Design and implement a console based Tic Tac Toe game....

    Tic Tac Toe Game: Help, please. Design and implement a console based Tic Tac Toe game. The objective of this project is to demonstrate your understanding of various programming concepts including Object Oriented Programming (OOP) and design. Tic Tac Toe is a two player game. In your implementation one opponent will be a human player and the other a computer player. ? The game is played on a 3 x 3 game board. ? The first player is known as...

  • I have already finished most of this assignment. I just need some help with the canMove...

    I have already finished most of this assignment. I just need some help with the canMove and main methods; pseudocode is also acceptable. Also, the programming language is java. Crickets and Grasshoppers is a simple two player game played on a strip of n spaces. Each space can be empty or have a piece. The first player plays as the cricket pieces and the other plays as grasshoppers and the turns alternate. We’ll represent crickets as C and grasshoppers as...

  • Project 7-3 Guessing Game import java.util.Scanner; public class GuessNumberApp {    public static void main(String[] args)...

    Project 7-3 Guessing Game import java.util.Scanner; public class GuessNumberApp {    public static void main(String[] args) { displayWelcomeMessage(); // create the Scanner object Scanner sc = new Scanner(System.in); String choice = "y"; while (choice.equalsIgnoreCase("y")) { // generate the random number and invite user to guess it int number = getRandomNumber(); displayPleaseGuessMessage(); // continue until the user guesses the number int guessNumber = 0; int counter = 1; while (guessNumber != number) { // get a valid int from user guessNumber...

  • I am unsure how to add the following methods onto this code?? please help - rowValuesIncrease(int[][]...

    I am unsure how to add the following methods onto this code?? please help - rowValuesIncrease(int[][] t) A method that returns true if from left to right in any row, the integers are increasing, otherwise false. - columnValuesIncrease(int[][] t) A method that returns true if from top to bottom in any column, the integers are increasing, otherwise false. - isSetOf1toN(int[][] t) A method that returns true if the set of integers used is {1, 2, . . . , n}...

  • Create a python add the following functions below to the module. Each section below is a...

    Create a python add the following functions below to the module. Each section below is a function that you must implement, make sure the function's names and parameters match the documentation (Copy-Paste). DO NOT put the functions in an if-name-main block. 1. def productSum(x: int, y: int, z: int) -> int This function should return: The product of x and y, if the product of x and y is less than z. Else it should return the sum of x...

  • Using an appropriate definition of ListNode, design a simple linked list class called StringList with the...

    Using an appropriate definition of ListNode, design a simple linked list class called StringList with the following member functions: void add (std::string); int positionOf (std::string); bool setNodeVal(int, std::string); std::vector<std::string> getAsVector(); a default constructor a copy constructor a destructor The add() function adds a new node containing the value of the parameter to the end of the list. The positionOf() function returns the (zero-based) position in the list for the first occurrence of the parameter in the list, or -1 if...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT