-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.rb
128 lines (123 loc) · 3.38 KB
/
run.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# Author: Jaime Martinez
# Contact: jaime.martinez88@gmail.com
# Date: June 21, 2016
require_relative './board'
require_relative './robot'
VALID_COMMANDS = ['PLACE','MOVE' ,'LEFT','RIGHT','REPORT','EXIT']
puts "Welcome!\n"
$newBoard = nil
$started = false
$robot = nil
# executeCommand runs the specified command, ignores invalid commands
def executeCommand(instruction)
cmd, rest = instruction.split
cmd = cmd.upcase
# stop program at anytime
if cmd =='EXIT' then
exit
# Allow PLACE command only at the beginning of the game
elsif !$started && cmd == 'PLACE' then #Check commands
placeRobot(rest)
elsif $started && VALID_COMMANDS.include?(cmd) then # Run valid commands
if cmd == 'MOVE' then
# Move robot
$robot.move($newBoard)
elsif cmd == 'LEFT' then
# turn LEFT
$robot.left()
elsif cmd == 'RIGHT'
#turn RIGHT
$robot.right()
elsif cmd == 'REPORT'
# REPORT status
puts $robot.report()
elsif cmd =='PLACE' then
placeRobot(rest)
end
else
puts "Invalid command '#{instruction}'"
end
end
# place a robot on the board, replaces any older robot
def placeRobot(position)
if position.nil? then
puts "Invalid number of arguments e.g. PLACE 1,2,NORTH"
return
end
# Get next arguments
x,y,dir = position.split(',')
x = x.to_i # assume x and y are Numeric types,
y = y.to_i # strings will be converted to 0
dir = dir.upcase
# Check argguments
if x.nil? || y.nil? || dir.nil? then
puts "Invalid number of arguments e.g. PLACE 1,2,NORTH"
elsif $newBoard.isValidPosition(x,y) then
if !Robot.isDirectionValid(dir) then
puts "Invalid direction #{dir}"
return
end
$started = true
$robot = Robot.new(x,y,dir) # => Robot object
else
puts "Invalid position. Board size is #{$newBoard.size}."
end
end
# Run the game until exit command or Ctrl+C
def main()
$init = false
# read from terminal
if ARGV.empty?
while true do
if !$init then
# Read a number from the terminal, only first argument counts
puts "Please select the size of the board (e.g. 5):\n"
boardSizeStr = gets.chomp
# exit program
boardSize = boardSizeStr.to_i
if boardSize == 0 && boardSizeStr.upcase =='EXIT' then
exit
elsif boardSize > 0 && boardSize <= 10 then
puts "New board size is #{boardSize} x #{boardSize}\n"
$newBoard = Board.new(boardSize)
$init = true
else
puts "Please use positive integer numbers from 1 to 10\n"
next
end
end
if !$started then
puts "Place a robot on the board! (e.g. PLACE 1,2,NORTH)\n"
else
puts "Availble commands #{VALID_COMMANDS}"
end
#Read commands from terminal
cmd = gets.chomp
executeCommand(cmd)
end
else
# Read from file
filename = ARGV[0]
if File.file?(filename) then # check if file exists
f = File.open(filename, "r")
f.each_line do |line|
puts line
if line == "" || line == "\n" then
next
elsif !$init then
boardSize = line.to_i
$newBoard = Board.new(boardSize)
$init = true
else
executeCommand(line)
end
end
f.close
else
puts "File '#{filename}' doesn't exist. Try again."
exit
end
end
end
# Run main program
main()