Git Product home page Git Product logo

itp-w3-tic-tac-toe's Introduction

OOP Tic Tac Toe

For this project, we're going to apply our knowledge of OOP to design a simple Tic Tac Toe game. The final code is going to be rather simple, what's really important is to design it carefully. Think about the design with your teammates (you can try drawing, brainstorming, etc).

Board

There are two big "parts" to solve in this project. The Board of the game is one of them. We've decided to build a dumb class as a board, that doesn't know much about the Tic Tac Toe game per se. It just has knowledge about its current state.

A Board, once initialized, will have a few handful methods like get_row, get_column and get_diagonal.

The tests for the board seem enough for you to understand it. For example, take a look at the test_row method. We just create a board, and we try to get the first row, which of course returns the elements on the first row of the board:

board = Board([
    ['X', None, None],
    ['O', 'X', None],
    [None, 'O', 'X'],
])
row_0 = board.get_row(0)
print(row_0)  # ['X', None, None]

col_0 = board.get_column(0)
print(col_0)  # ['X', 'O', None]

The most important thing about the Board class, is how you design the interal data structure of your board. How are you going to store the elements of your board. That's going to have a profound impact in your end solution. Example: if you choose a list of lists (as the code above) or a dictionary:

board_data_as_lists = [
    ['X', None, None],
    ['O', 'X', None],
    [None, 'O', 'X'],
]
board_data_as_dict = {
    0: ['X', None, None],
    1: ['O', 'X', None],
    2: [None, 'O', 'X'],
}

Game

Now we get into the actual Tic Tac Toe game. The Game class is the one that contains all the business logic to play the game. The Board, for example, didn't know when there was a winner, because that's business logic. In Tic Tac Toe you have a winner if all the elements in a row are the same: again, that's business logic.

A Game will use a Board class to store all its data, and it'll add the business logic on top of it. A Game will be the main class the user interacts with. For example: whenever you want to make a move, you'll do game.move(...) that internally will invoke board.move(...). The difference is that the Game class will add all the business logic to that move: check if the position is already taken, check if the game is already over, etc.

A game will also have a set of utility methods to check the state of the game: is_finished(), has_winner(), get_winner(), is_tied().

itp-w3-tic-tac-toe's People

Contributors

santiagobasulto avatar kabayomi avatar martinzugnoni avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.