code_legend
code_legend

Reputation: 3285

Checked if the coordinates of a position are within a boundaries

I have created a function that is trying to figure out if the coordinates of a position are within the checkerboard boundaries (between 0 inclusive and the number

Below is my attempt:

def position_is_in_game(self, position):


    # Replace self with a Game type object
    one_game = Game()

    #Replace position with an object of type Position
    one_position = Position()

    return one_game.position_is_in_game(one_position)

I feel my code is incomplete.

Upvotes: 0

Views: 273

Answers (2)

Gunty
Gunty

Reputation: 2479

Assuming that your Position and Piece functions are coded correctly, the issue lies within the checking algorithm's syntax.

Try it like this:

def position_is_in_game(position_tuple):
    game = Game()

    game_positions = game.cases.keys()
    for i in game_positions:
        if i == position_tuple:
            return True
        else:
            return False

Upvotes: 1

bashBedlam
bashBedlam

Reputation: 1500

Here is how I would check to see if a position in on the gird:

class Game :
    def __init__(self):
        self.n_lignes = 8
        self.n_colonnes = 8

    def position_is_in_game(self, x, y):
        return x > 0 and x < self.n_lignes and y > 0 and y < self.n_colonnes

mygame = Game ()
print (mygame.position_is_in_game(1, 8))

Upvotes: 1

Related Questions