3 # This file is part of NIT ( http://www.nitlanguage.org ).
4 # This program is public domain
7 # SEE: <http://rosettacode.org/wiki/24_game>
10 fun is_op
: Bool do return "-+/*".has
(self)
13 # Get `numbers` and `operands` from string `operation` collect with `gets` in `main` function
14 # Fill `numbers` and `operands` array with previous extraction
15 fun exportation
(operation
: String, numbers
: Array[Int], operands
: Array[Char]) do
16 var previous_char
: nullable Char = null
17 var number
: nullable Int = null
20 for i
in operation
.length
.times
do
21 var current_char
= operation
[i
]
22 var current_int
= current_char
.to_i
24 if (previous_char
== null or previous_char
.is_op
) and current_char
== '-' then
29 if current_char
.is_digit
then
30 if number
== null then
33 number
= number
* 10 + current_int
37 if negative
and (current_char
.is_op
or i
== operation
.length
- 1) then
38 number
= number
- number
* 2
42 if (current_char
.is_op
or i
== operation
.length
- 1) and number
!= null then
47 if not negative
and current_char
.is_op
then
48 operands
.add
(current_char
)
50 previous_char
= current_char
52 # Update `numbers` and `operands` array in main function with pointer
55 # Create random numbers between 1 to 9
56 fun random
: Array[Int] do
57 return [for i
in 4.times
do 1 + 9.rand
]
60 # Make mathematical operation with `numbers` and `operands` and add the operation result into `random_numbers`
61 fun calculation
(random_numbers
, numbers
: Array[Int], operands
: Array[Char]) do
63 var temp_numbers
= numbers
.clone
65 while temp_numbers
.length
> 1 do
66 var operand
= operands
.shift
67 var a
= temp_numbers
.shift
68 var b
= temp_numbers
.shift
70 if operand
== '+' then number
= a
+ b
71 if operand
== '-' then number
= a
- b
72 if operand
== '*' then number
= a
* b
73 if operand
== '/' then number
= a
/ b
75 temp_numbers
.unshift
(number
)
77 if number
!= 0 then random_numbers
.add
(number
)
80 # Check if used `numbers` exist in the `random_numbers` created
81 fun numbers_exists
(random_numbers
, numbers
: Array[Int]): Bool do
82 for number
in numbers
do
83 if not random_numbers
.count
(number
) >= numbers
.count
(number
) then return false
88 # Remove `numbers` when they are used
89 fun remove_numbers
(random_numbers
, numbers
: Array[Int]) do
90 for number
in numbers
do random_numbers
.remove
(number
)
93 # Check if the mathematical `operation` is valid
94 fun check
(operation
: String): Bool do
95 var previous_char
: nullable Char = null
96 var next_char
: nullable Char = null
97 var next_1_char
: nullable Char = null
99 for i
in operation
.length
.times
do
100 var current_char
= operation
[i
]
102 if i
+ 1 < operation
.length
then
103 next_char
= operation
[i
+ 1]
104 if i
+ 2 < operation
.length
then
105 next_1_char
= operation
[i
+ 2]
113 if not current_char
.is_op
and not current_char
.is_digit
then return false
114 if next_char
== null and current_char
.is_op
then return false
116 if previous_char
== null then
117 if next_char
== null or next_1_char
== null then return false
118 if current_char
== '-' and not next_char
.is_digit
then return false
119 if current_char
!= '-' and not current_char
.is_digit
then return false
121 if next_char
!= null then
122 if previous_char
.is_digit
and current_char
.is_op
and
123 not (next_char
== '-' and next_1_char
!= null and
124 next_1_char
.is_digit
or next_char
.is_digit
) then
129 previous_char
= current_char
134 var random_numbers
= new Array[Int]
137 random_numbers
= random
138 while not random_numbers
.has
(24) and random_numbers
.length
> 1 do
139 var numbers
= new Array[Int]
140 var operands
= new Array[Char]
142 print
"numbers: " + random_numbers
.join
(", ")
144 if check
(operation
) then
145 exportation
(operation
, numbers
, operands
)
146 if numbers_exists
(random_numbers
, numbers
) then
147 calculation
(random_numbers
, numbers
, operands
)
148 remove_numbers
(random_numbers
, numbers
)
150 print
"NUMBERS ERROR!"
153 print
"STRING ERROR!"
157 if random_numbers
.has
(24) then print
"CONGRATULATIONS" else print
"YOU LOSE"