Python Forum
Requesting homework help and maybe a little code review… - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Requesting homework help and maybe a little code review… (/thread-23775.html)



Requesting homework help and maybe a little code review… - JonnyEnglish - Jan-16-2020

Hi guys,

Homework Help
I want to put the numbers 0 - 3 around my game board to represent the row and column indexes like so:
0 1 2 3
0 * * * *
1 * * * *
2 * * * *
3 * * * *
But I’m drawing a blank. Can I get a hint as to how might be best to solve this problem please?

Code Review
I’m hoping I could get some feedback on my attempt at a game called pairs. A detailed brief for the task can be found here:pairs google doc

Anyways, thanks for taking the time to read my post

import random
import sys
import time

import pyautogui


def cls():
    pyautogui.hotkey('ctrl', 'l')


def create_answer_board(cards, row=4, col=4):
    return [cards[table_row * col:(table_row + 1) * col] for table_row in range(0, row)]


def create_concealed_board():
    return [['X' for col in range(4)] for row in range(4)]


def display_board(board):
    cards_in_row = ''
    for row in board:
        for card in row:
            cards_in_row += card + ' '
        print(cards_in_row)
        cards_in_row = ''
    print('')


def select_card(concealed_board):
    try:
        row, col = [int(x) for x in
                    input(
                        "Please enter the row and column number (separated by a space and in that order) for the card you want to flip over:").split()]
        if row > 3 or col > 3 or row < 0 or col < 0:
            print("Input is outside the given range.")
            return select_card(concealed_board)
        if is_hidden_card(concealed_board, row, col):
            return [row, col]
        else:
            print("That card has been revealed... Please try again.")
            return select_card(concealed_board)
    except ValueError:
        print("Please try again:")
        return select_card(concealed_board)


def reveal_selected_card(selected_card_location, concealed_board):
    concealed_board[selected_card_location[0]][selected_card_location[1]] = cards[selected_card_location[0]][
        selected_card_location[1]]
    display_board(concealed_board)


def check_for_match(selected_card_location_1, selected_card_location_2, cards):
    card_1 = cards[selected_card_location_1[0]][selected_card_location_1[1]]
    card_2 = cards[selected_card_location_2[0]][selected_card_location_2[1]]
    return card_1 == card_2


def is_hidden_card(concealed_board, row, col):
    return concealed_board[row][col] == '_'


def insert_X(selected_card_location_1, selected_card_location_2, concealed_board):
    concealed_board[selected_card_location_1[0]][selected_card_location_1[1]] = "X"
    concealed_board[selected_card_location_2[0]][selected_card_location_2[1]] = "X"
    return concealed_board


def is_complete(concealed_board):
    it = iter(concealed_board)
    for first in it:
        break
    else:
        return True  # empty case, note all([]) == True
    return all(x == first for x in it)


cards = ["J", "J", "J", "J", "Q", "Q", "Q", "Q", "K", "K", "K", "K", "A", "A", "A", "A"]
random.shuffle(cards)

cards = create_answer_board(cards)
concealed_board = create_concealed_board()

while True:
    display_board(cards) # just here for testing
    display_board(concealed_board)
    selected_card_location_1 = select_card(concealed_board)
    reveal_selected_card(selected_card_location_1, concealed_board)
    selected_card_location_2 = select_card(concealed_board)
    reveal_selected_card(selected_card_location_2, concealed_board)
    if check_for_match(selected_card_location_1, selected_card_location_2, cards):
        print("Congrats, they matched")
        print("Clearing the screen in 3 seconds")
        time.sleep(3)
        concealed_board = insert_X(selected_card_location_1, selected_card_location_2, concealed_board)
        cls()
    if is_complete(concealed_board):
        print("Congrats you found all 8 pairs and have won the game!!!")
        sys.exit()

hey,
I solved homework help by modifying the display_board function to this:

def display_board(board):
    cards_in_row = ''
    number = 0
    print("    0 1 2 3")
    for row in board:
        print(number, end='   ')
        number += 1
        for card in row:
            cards_in_row += card + ' '
        print(cards_in_row)
        cards_in_row = ''
    print('')