GithubHelp home page GithubHelp logo

tic_tac_toe's Introduction

Tic-Tac-Toe

This application is to build the famous tic-tac-toe game. The board is a 3 X 3 grid and players alternate turns until one player is victorious or the game ends in a draw. Players win by securing three consecutive positions on a row, column, or diagonal. The game ends in a tie if neither player has won and all positions on the board are taken.

###Installing

Ensure you have Git and Ruby 2.2.2 installed. Optionally, install rbenv to manage Ruby versions, and editorconfig for automatic editor configuration.

Then, in a console execute:

git clone [email protected]:judywu29/tic_tac_toe.git
cd tic_tac_toe
gem install bundler
bundle install

###Supported operating systems

tic-tac-toe should install on pretty much any recent Linux or OSX operating system.

###Running

Interactively:

$ ruby tic_tac_toe.rb
Welcome to tic tac toe
X has randomly been selected as the first player
pls enter a number between 1 and 9 to make your move
1
O, your turn. 
6
X, your turn. 
2
O, your turn. 
7
X, your turn. 
3
X won!

With invalid commands:

$ ruby tic_tac_toe.rb
Welcome to tic tac toe
X has randomly been selected as the first player
pls enter a number between 1 and 9 to make your move
a
Ignoring this invalid step, pls try again.
10
Ignoring this invalid step, pls try again.
abc
Ignoring this invalid step, pls try again.
 1 
Ignoring this invalid step, pls try again.
1
O, your turn.

###Development notes

I made the following assumptions for the std input:

  • Lines that are either empty or contain only whitespace can be completely ignored
  • Whitespace in line is invalid

I assume the grid is an Array with three nested Arrays, each with three elements.

The nested array data structure closely mirrors an actual tic-tac-toe board:

example_array = [ ["X", "O", "X"], ["O", "O", " "], ["X", "O", "1"] ] A nested data structure also provides a convenient coordinate system to access elements of the array. Coordinate systems are usually stated in (x, y) and have an origin in the lower left corner. The nested array coordinate system has an origin in the upper left corner and is stated in (y, x). To access the โ€œ1โ€ element in the example_array above, we use this code: example_array[2][2]. Make sure to play with the nested array structure in your console until you understand it.

The Game class is responsible for creating a message that can be printed on the command line and ask the current_player to make a move. The human move will be a number from 1 to 9 that corresponds with a cell on the grid. I used a mapping like this to simplify the move for the users: mapping = { "1" => [0, 0], "2" => [1, 0], "3" => [2, 0], "4" => [0, 1], "5" => [1, 1], "6" => [2, 1], "7" => [0, 2], "8" => [1, 2], "9" => [2, 2] }

Depending upon the time available and the background of the intended users, some of those assumptions could easily be challenged. E.g. Windows users are not used to case sensitivity or silent success.

###Contact

Judy Wu, [email protected]

tic_tac_toe's People

Contributors

judywu29 avatar

Watchers

James Cloos 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.