rutictactoe 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/lib/board.rb +48 -0
- data/lib/game.rb +38 -0
- data/lib/player.rb +11 -0
- data/lib/rutictactoe.rb +3 -0
- data/lib/user_interface.rb +89 -0
- metadata +47 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 95be2896817cb677c1d465cb5dd4c42eef3ce4837707aeccade2d7f49cc0116d
|
|
4
|
+
data.tar.gz: 51a0ecca9f3f0cd275176f139866eb896ae638df41dac268ba699e8a102dcdd0
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 8e197219d6511b316b4171e55f66a0f4521372839fff5399547463f352b7615310667c9dbc742494104a919b11e9904798d3999f9063012ec66d110c38d2d1a3
|
|
7
|
+
data.tar.gz: ad52db1935f7a9a1dd89800da47725b294d16ce38f73514e403c40480d921881d994a2254802ab1e99f626e343c073543f7be5374d4eb2113020c6f3d96e56be
|
data/lib/board.rb
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
class Board
|
|
2
|
+
def initialize
|
|
3
|
+
@board = [0, 1, 2, 3, 4, 5, 6, 7, 8]
|
|
4
|
+
end
|
|
5
|
+
|
|
6
|
+
def fill(index, symbol)
|
|
7
|
+
@board[index] = symbol
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def filled?(index)
|
|
11
|
+
@board[index] != index
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def get_display
|
|
15
|
+
" | | \n" +
|
|
16
|
+
" #{@board[0]} | #{@board[1]} | #{@board[2]} \n" +
|
|
17
|
+
"___|___|___\n" +
|
|
18
|
+
" | | \n" +
|
|
19
|
+
" #{@board[3]} | #{@board[4]} | #{@board[5]} \n" +
|
|
20
|
+
"___|___|___\n" +
|
|
21
|
+
" | | \n" +
|
|
22
|
+
" #{@board[6]} | #{@board[7]} | #{@board[8]} \n" +
|
|
23
|
+
' | | '
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def draw?
|
|
27
|
+
@board.each do |val|
|
|
28
|
+
return false if val.instance_of?(Integer)
|
|
29
|
+
end
|
|
30
|
+
true
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def end?
|
|
34
|
+
b = @board
|
|
35
|
+
[
|
|
36
|
+
[0, 1, 2], [3, 4, 5], [6, 7, 8], [0, 3, 6],
|
|
37
|
+
[1, 4, 7], [2, 5, 8], [0, 4, 8], [2, 4, 6]
|
|
38
|
+
].each do |position|
|
|
39
|
+
x, y, z = position
|
|
40
|
+
return true if b[x] == b[y] && b[x] == b[z]
|
|
41
|
+
end
|
|
42
|
+
false
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def reset
|
|
46
|
+
@board = [0, 1, 2, 3, 4, 5, 6, 7, 8]
|
|
47
|
+
end
|
|
48
|
+
end
|
data/lib/game.rb
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
require_relative 'board'
|
|
2
|
+
require_relative 'player'
|
|
3
|
+
require_relative 'user_interface'
|
|
4
|
+
|
|
5
|
+
class Game
|
|
6
|
+
def initialize
|
|
7
|
+
@board = Board.new
|
|
8
|
+
@ui = UserInterface.new
|
|
9
|
+
@turn = 0
|
|
10
|
+
|
|
11
|
+
p1n, p1s, p2n, p2s = @ui.get_player_data
|
|
12
|
+
@player_one = Player.new(p1n, p1s, true)
|
|
13
|
+
@player_two = Player.new(p2n, p2s, false)
|
|
14
|
+
game_loop
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def game_loop
|
|
18
|
+
active_player = @turn.even? ? @player_one : @player_two
|
|
19
|
+
@ui.display_turn(@board, active_player)
|
|
20
|
+
move_position = @ui.get_move(@board, active_player)
|
|
21
|
+
@board.fill(move_position, active_player.symbol)
|
|
22
|
+
|
|
23
|
+
return end_game(active_player) if @board.end?
|
|
24
|
+
return end_game if @board.draw?
|
|
25
|
+
|
|
26
|
+
@turn += 1
|
|
27
|
+
game_loop
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def end_game(winner = nil)
|
|
31
|
+
@ui.display_end(@board, winner)
|
|
32
|
+
return unless @ui.play_again?
|
|
33
|
+
|
|
34
|
+
@board.reset
|
|
35
|
+
@turn = 0
|
|
36
|
+
game_loop
|
|
37
|
+
end
|
|
38
|
+
end
|
data/lib/player.rb
ADDED
data/lib/rutictactoe.rb
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
class UserInterface
|
|
2
|
+
def initialize
|
|
3
|
+
clear
|
|
4
|
+
puts 'Welcome to Tic Tac Toe!'
|
|
5
|
+
puts 'by Yosua Nicolaus 2023'
|
|
6
|
+
puts ''
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def get_player_data
|
|
10
|
+
puts 'Player 1 name: '
|
|
11
|
+
p1name = gets.chomp
|
|
12
|
+
puts 'Player 1 symbol: <A-Z>'
|
|
13
|
+
p1symb = get_input_symbol
|
|
14
|
+
|
|
15
|
+
puts 'Player 2 name: '
|
|
16
|
+
p2name = gets.chomp
|
|
17
|
+
puts 'Player 2 symbol: <A-Z>'
|
|
18
|
+
p2symb = get_input_symbol(p1symb)
|
|
19
|
+
|
|
20
|
+
[p1name, p1symb, p2name, p2symb]
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def display_turn(board, player)
|
|
24
|
+
clear
|
|
25
|
+
puts "#{player.name}'s turn <#{player.symbol}>"
|
|
26
|
+
puts board.get_display
|
|
27
|
+
puts ''
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def get_move(board, player)
|
|
31
|
+
puts "@#{player.name} - Enter a number (0 - 8)"
|
|
32
|
+
input = gets.chomp.to_i
|
|
33
|
+
|
|
34
|
+
if input < 0 || input > 8
|
|
35
|
+
puts 'Number has to be between 0-8!'
|
|
36
|
+
return get_move(board, player)
|
|
37
|
+
elsif board.filled?(input)
|
|
38
|
+
puts "Index #{input} has been filled!"
|
|
39
|
+
return get_move(board, player)
|
|
40
|
+
end
|
|
41
|
+
input
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def display_end(board, winner = nil)
|
|
45
|
+
clear
|
|
46
|
+
puts '------------------------------'
|
|
47
|
+
puts board.get_display
|
|
48
|
+
puts 'Game End!'
|
|
49
|
+
if winner.nil?
|
|
50
|
+
puts 'Result - Draw'
|
|
51
|
+
else
|
|
52
|
+
puts "Result - #{winner.name} win!"
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def play_again?
|
|
57
|
+
puts 'Would you like to play again? [Y/n]'
|
|
58
|
+
answer = gets.chomp[0]
|
|
59
|
+
|
|
60
|
+
if answer.nil? || answer.upcase == 'Y'
|
|
61
|
+
true
|
|
62
|
+
elsif answer.upcase == 'N'
|
|
63
|
+
puts 'Thanks for playing!'
|
|
64
|
+
false
|
|
65
|
+
else
|
|
66
|
+
play_again?
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
private def get_input_symbol(previous_symbol = nil)
|
|
71
|
+
input = gets.chomp
|
|
72
|
+
|
|
73
|
+
if input.length != 1
|
|
74
|
+
puts 'Symbol has to be of 1 letter!'
|
|
75
|
+
return get_input_symbol
|
|
76
|
+
elsif input.ord < 65 || input.ord > 90
|
|
77
|
+
puts 'Symbol has to be between A-Z range!'
|
|
78
|
+
return get_input_symbol
|
|
79
|
+
elsif input == previous_symbol
|
|
80
|
+
puts 'Symbol has been taken!'
|
|
81
|
+
return get_input_symbol
|
|
82
|
+
end
|
|
83
|
+
input
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
private def clear
|
|
87
|
+
puts `clear`
|
|
88
|
+
end
|
|
89
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: rutictactoe
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Yosua Nicolaus
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2023-02-11 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description: Play Tic Tac Toe in your command line!
|
|
14
|
+
email: yosuanicolaus@gmail.com
|
|
15
|
+
executables: []
|
|
16
|
+
extensions: []
|
|
17
|
+
extra_rdoc_files: []
|
|
18
|
+
files:
|
|
19
|
+
- "./lib/board.rb"
|
|
20
|
+
- "./lib/game.rb"
|
|
21
|
+
- "./lib/player.rb"
|
|
22
|
+
- "./lib/user_interface.rb"
|
|
23
|
+
- lib/rutictactoe.rb
|
|
24
|
+
homepage: https://rubygems.org/gems/hola
|
|
25
|
+
licenses:
|
|
26
|
+
- MIT
|
|
27
|
+
metadata: {}
|
|
28
|
+
post_install_message:
|
|
29
|
+
rdoc_options: []
|
|
30
|
+
require_paths:
|
|
31
|
+
- lib
|
|
32
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
33
|
+
requirements:
|
|
34
|
+
- - ">="
|
|
35
|
+
- !ruby/object:Gem::Version
|
|
36
|
+
version: '0'
|
|
37
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
38
|
+
requirements:
|
|
39
|
+
- - ">="
|
|
40
|
+
- !ruby/object:Gem::Version
|
|
41
|
+
version: '0'
|
|
42
|
+
requirements: []
|
|
43
|
+
rubygems_version: 3.3.7
|
|
44
|
+
signing_key:
|
|
45
|
+
specification_version: 4
|
|
46
|
+
summary: Command Line Tic Tac Toe Game
|
|
47
|
+
test_files: []
|