Skip to content
Open

Done #807

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
4 changes: 2 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ gem 'turbolinks'

group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
# gem 'byebug'
gem 'byebug'
gem 'rspec-rails', '~> 3.0'
gem 'pry'


# Adds step-by-step debugging and stack navigation capabilities to pry using byebug. To use, invoke pry normally.
# gem 'pry-byebug'
Expand Down
9 changes: 3 additions & 6 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ GEM
binding_of_caller (0.8.0)
debug_inspector (>= 0.0.1)
builder (3.2.3)
byebug (11.0.1)
case_transform (0.2)
activesupport
coderay (1.1.2)
coffee-rails (4.1.1)
coffee-script (>= 2.2.0)
railties (>= 4.0.0, < 5.1.x)
Expand Down Expand Up @@ -85,9 +85,6 @@ GEM
nio4r (2.4.0)
nokogiri (1.10.3)
mini_portile2 (~> 2.4.0)
pry (0.12.2)
coderay (~> 1.1.0)
method_source (~> 0.9.0)
rack (2.0.7)
rack-test (0.6.3)
rack (>= 1.0)
Expand Down Expand Up @@ -179,9 +176,9 @@ PLATFORMS

DEPENDENCIES
active_model_serializers (= 0.10.6)
byebug
coffee-rails (~> 4.1.0)
jquery-rails
pry
rails (~> 5.0)
rspec-rails (~> 3.0)
sass-rails (~> 5.0)
Expand All @@ -192,4 +189,4 @@ DEPENDENCIES
web-console (~> 2.0)

BUNDLED WITH
2.0.1
2.0.2
131 changes: 131 additions & 0 deletions NOTES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
// this lab doesn't like let and const
$(document).ready(function() {
attachListeners();
});

var currentGame = 0;
var turn =0;
var win_combo =[
[0,1,2],
[3,4,5],
[6,7,8],
[0,3,6],
[1,4,7],
[2,5,8],
[0,4,8],
[2,4,6]
];

// Player
function player() {
if (turn % 2 === 0) {
return 'X';
} else {
return 'O';
}
};

// UpdateState
function updateState(squares) {
squares.innerHTML = player();
}

// SetMessage
function setMessage(string){
var message = document.getElementById('message')
message.innerHTML = string
}

function checkWinner() {

var board = {};
var winner = false;


// we grab the td element (which is an array type object), .text() iterates through,
// and with the arrow function, assigns the value of the square to the board
$('td').text((index, square) => board[index] = square);

win_combo.forEach(function(position) {
if (board[position[0]] !== "" && board[position[0]] === board[position[1]] && board[position[1]] === board[position[2]]) {
setMessage(`Player ${board[position[0]]} Won!`);
return winner = true;
}
});
return winner;
}

function doTurn(square) {
updateState(square);
turn++;
if (checkWinner()) {
saveGame();
resetBoard();
} else if (turn === 9) {
setMessage('Tie game.');
saveGame();
resetBoard();
}
}

function saveGame() {
var state = []
$('td').text((index, square) => state[index] = square);

if(currentGame){
$.ajax({
type: 'PATCH',
url: `/games/${currentGame}`,
data: state
});
} else {
$.post(`/games`,function(data){
currentGame = data["data"]["id"]
alert(currentGame)
});
}
}


function resetBoard() {
$('td').empty();
turn = 0;
currentGame = 0;
}

function attachListeners(){
$( "td" ).on( "click", function() {
if(!$.text(this) && !checkWinner()) {
doTurn(this);
};
});
// button#previous
// $("#previous").one("click", previousGame);
var previous = window.document.getElementById('previous')
// Callback function
previous.addEventListener('click', previousGame);
//button#save
var saveButton = window.document.getElementById('save');
saveButton.addEventListener('click', saveGame);
//button#clear
var clearButton = window.document.getElementById('clear');
clearButton.addEventListener('click', resetBoard);
}

function previousGame(){
var divGames = document.getElementById('games');

$.get("/games", function(data) {
var array = data["data"]
array.forEach(function(element){
var newButton = document.createElement('button');
newButton.innerHTML = element.id
divGames.appendChild(newButton);
return saveGame;
// Add conditional so already existing games don't get added to
// the list of buttons


})
})
}
128 changes: 128 additions & 0 deletions NOTES.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
board = [" "," "," "," "," "," "," "," "," "]

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

# display_board
def display_board(board)
puts " #{board[0]} | #{board[1]} | #{board[2]} "
puts "-----------"
puts " #{board[3]} | #{board[4]} | #{board[5]} "
puts "-----------"
puts " #{board[6]} | #{board[7]} | #{board[8]} "
end

# input_to_index
def input_to_index(user_input)
user_input.to_i - 1
end

# move
def move(board, position, char)
board[position] = char
end

# position_taken
def position_taken?(board, index_i)
((board[index_i] == "X") || (board[index_i] == "O"))
end

# valid_move
def valid_move?(board, index)
index.between?(0,8) && !position_taken?(board, index)
end

#turn
def turn(board)
puts "Please enter 1-9:"
input = gets.strip
index = input_to_index(input)
char = current_player(board)
if valid_move?(board, index)
move(board, index, char)
display_board(board)
else
turn(board)
end
end

# turn_count
def turn_count(board)
number_of_turns = 0
board.each do |space|
if space == "X" || space == "O"
number_of_turns += 1
end
end
return number_of_turns
end

# current_player
def current_player(board)
if turn_count(board) % 2 == 0
"X"
else
"O"
end
end

# won?
def won?(board)
WIN_COMBINATIONS.detect do |win_combo|
if (board[win_combo[0]]) == "X" && (board[win_combo[1]]) == "X" && (board[win_combo[2]]) == "X"
return win_combo
elsif (board[win_combo[0]]) == "O" && (board[win_combo[1]]) == "O" && (board[win_combo[2]]) == "O"
return win_combo
end
false
end
end

#full?
def full?(board)
board.all?{|occupied| occupied != " "}
end

#draw?
def draw?(board)
!(won?(board)) && (full?(board))
end

#over?
def over?(board)
(won?(board)) || (full?(board)) || (draw?(board))
end

#winner?
def winner(board)
WIN_COMBINATIONS.detect do |win_combo|
if (board[win_combo[0]]) == "X" && (board[win_combo[1]]) == "X" && (board[win_combo[2]]) == "X"
return "X"
elsif (board[win_combo[0]]) == "O" && (board[win_combo[1]]) == "O" && (board[win_combo[2]]) == "O"
return "O"
else
nil
end
end
end

# play
def play(board)
while over?(board) == false
turn(board)
end
if won?(board)
puts "Congratulations #{winner(board)}!"
elsif draw?(board)
puts "Cats Game!"
end
end
Loading