Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

scoreboard and restart added #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@
</head>

<body>

<!-- score board -->
<div class = 'score'>
<span class = 'total-score' id = "total">Total score
<span class = 'human-score' id = "human-score">0</span>
<span class = 'robot-score' id = "robot-score">0</span>
</span>
</div>

<div class = 'main-container'>
<div class = 'board'>
<!-- data-indx following cell divs represents cell index in 1D array representation -->
Expand Down
38 changes: 25 additions & 13 deletions scripts/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,23 +112,28 @@ var State = function(old) {
*/
var Game = function(autoPlayer) {

//public : initialize the ai player for this game
this.ai = autoPlayer;
// init new game
this.init = function() {

// public : initialize the game current state to empty board configuration
this.currentState = new State();
//public : initialize the ai player for this game
this.ai = autoPlayer;

//"E" stands for empty board cell
this.currentState.board = ["E", "E", "E",
"E", "E", "E",
"E", "E", "E"];
// public : initialize the game current state to empty board configuration
this.currentState = new State();

this.currentState.turn = "X"; //X plays first
//"E" stands for empty board cell
this.currentState.board = ["E", "E", "E",
"E", "E", "E",
"E", "E", "E"];

/*
* initialize game status to beginning
*/
this.status = "beginning";
this.currentState.turn = "X"; //X plays first

/*
* initialize game status to beginning
*/
this.status = "beginning";

}

/*
* public function that advances the game to a new state
Expand All @@ -139,6 +144,9 @@ var Game = function(autoPlayer) {
if(_state.isTerminal()) {
this.status = "ended";

//update score
ui.updateScore(_state.result);

if(_state.result === "X-won")
//X won
ui.switchViewTo("won");
Expand All @@ -148,6 +156,9 @@ var Game = function(autoPlayer) {
else
//it's a draw
ui.switchViewTo("draw");

//start a new game
ui.restartGame(this);
}
else {
//the game is still running
Expand All @@ -168,6 +179,7 @@ var Game = function(autoPlayer) {
* starts the game
*/
this.start = function() {
this.init();
if(this.status = "beginning") {
//invoke advanceTo with the initial state
this.advanceTo(this.currentState);
Expand Down
27 changes: 27 additions & 0 deletions scripts/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,33 @@ ui.switchViewTo = function(turn) {
}
};

/*
* updates score table
* @param _status [String]: the last round winner
*/
ui.updateScore = function(_status) {
if (_status === "X-won") {
$('.human-score').text(parseInt($('.human-score').text()) + 1);
} else if (_status === "O-won") {
$('.robot-score').text(parseInt($('.robot-score').text()) + 1);
}
}

/*
* restarts board
*/
ui.restartGame = function(newGame) {
var board = $('.cell');

board.fadeOut(1200);
setTimeout(function() { board.text(""); }, 620);
board.fadeIn(function() {
$(board).removeClass('occupied');
$(board).addClass('undefined');
newGame.start(); }
);
}

/*
* places X or O in the specifed place in the board
* @param i [Number] : row number (0-indexed)
Expand Down
29 changes: 29 additions & 0 deletions styles/ui.css
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,35 @@
margin: 0 auto;
}

.score {
display: inline-block;
height: 200px;
width: 200px;
float:left;
line-height: 60px;
margin-top: 40px;
text-align: center;
}

.total-score {
color: lightskyblue;
font-family: "Arial Narrow";
font-size: 40px;
font-weight: bold;
}

.human-score {
float: left;
margin-left: 65px;
font-size: 45px;
}

.robot-score {
float: right;
margin-right: 65px;
font-size: 45px;
}

.board {
height: 420px;
width: 420px;
Expand Down