Giter Site home page Giter Site logo

oop-tic-tac-toe-james-hwang's Introduction

Tic-Tac-Toe

Challenge

To create an Object Oriented Tic-Tac-Toe game, where the board size is chosen by the user and the winning pattern is NOT hard coded.

Install & Run

  1. Clone the repository
  2. bundle install
  3. run ruby bin/tic-tac-toe to play

Included Features

  • Playable tic-tac-toe game against computer.
  • Allowing user to choose who becomes O or X.
  • Winner announcement and a choice to play again or quit.
  • Variable Grid Size (User can choose how big the board is.)
  • Randomly choosing who starts the game first.

Win Detection Algorithm

Because the grid size of the game is variable, it is not possible to hard code the entire winning results of both computer and the player.

Therefore, we need to keep track of how many O or X has been placed in each row and column. I have created 2-dimensional arrays to separate and easily keep track of each moves.

For example, in typical 3 x 3 tic-tac-toe game, the board looks like this.

board = [
  [0, 1, 2],
  [3, 4, 5],
  [6, 7, 8]
]

With the above setup, we can have a score tracker that is the same length as the board, which each element represents the row or column.

row_stats = [0, 0, 0]

column_stats = [0, 0, 0]

Then, with every coordinate that is given by the player, we can take the first index of the coordinate to update our row_stats to keep track of the moves.

player gives the coordinate [0,1]

update the stats as -> row_stats = [1, 0, 0] and column_stats = [0, 1, 0]

When the stats are equal to the board.length, we know that the row or the column has been fully filled by the player, which he or she should be the winner.

Also, there could be only 1 possible outcome for each diagonal and inverse-diagonal.

For diagonal wins:

With every move, if x-coordinate and y-coordinate value is same, then we increment the diagonal value since diagonal wins can only be achieved by the same coordinates.

diagonal_stats = 0

player gives [0,0]

update diagonal_stats to diagonal_stats = 1

When the stats is equal to the board.length, we have the winner by the diagonal.

For inverse-diagonal wins:

With every move, if x-coordinate and y-coordinate's total is equal to board.length, then we increment the inverse-diagonal value since inverse-diagonal points are always equal to the board.length.

inverse_diagonal_stats = 0

player gives [0,2]

0 + 2 + 1 = 3

update diagonal_stats to inverse_diagonal_stats = 1

oop-tic-tac-toe-james-hwang's People

Contributors

91juhwang 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.