|
| 1 | +# REQUIRES: has_ruby |
| 2 | +# RUN: split-file %s %t |
| 3 | + |
| 4 | +# RUN: rlc %t/source.rl -o %t/lib%sharedext -i %stdlib --shared |
| 5 | +# RUN: rlc %t/source.rl -o %t/lib%libext -i %stdlib --compile |
| 6 | + |
| 7 | +# RUN: rlc %t/source.rl -o %t/library.rb -i %stdlib --ruby |
| 8 | +# RUN: ruby %t/to_run.rb |
| 9 | + |
| 10 | + |
| 11 | +#--- source.rl |
| 12 | +import serialization.to_byte_vector |
| 13 | +import string |
| 14 | +import action |
| 15 | +import learn |
| 16 | + |
| 17 | +cls Board: |
| 18 | + BInt<0, 3>[9] slots |
| 19 | + Bool playerTurn |
| 20 | + |
| 21 | + fun get(Int x, Int y) -> Int: |
| 22 | + return self.slots[x + (y * 3)].value |
| 23 | + |
| 24 | + fun set(Int x, Int y, Int val): |
| 25 | + self.slots[x + (y * 3)].value = val |
| 26 | + |
| 27 | + fun full() -> Bool: |
| 28 | + let x = 0 |
| 29 | + |
| 30 | + while x < 3: |
| 31 | + let y = 0 |
| 32 | + while y < 3: |
| 33 | + if self.get(x, y) == 0: |
| 34 | + return false |
| 35 | + y = y + 1 |
| 36 | + x = x + 1 |
| 37 | + |
| 38 | + return true |
| 39 | + |
| 40 | + fun three_in_a_line_player_row(Int player_id, Int row) -> Bool: |
| 41 | + return self.get(0, row) == self.get(1, row) and self.get(0, row) == self.get(2, row) and self.get(0, row) == player_id |
| 42 | + |
| 43 | + fun three_in_a_line_player(Int player_id) -> Bool: |
| 44 | + let x = 0 |
| 45 | + while x < 3: |
| 46 | + if self.get(x, 0) == self.get(x, 1) and self.get(x, 0) == self.get(x, 2) and self.get(x, 0) == player_id: |
| 47 | + return true |
| 48 | + |
| 49 | + if self.three_in_a_line_player_row(player_id, x): |
| 50 | + return true |
| 51 | + x = x + 1 |
| 52 | + |
| 53 | + if self.get(0, 0) == self.get(1, 1) and self.get(0, 0) == self.get(2, 2) and self.get(0, 0) == player_id: |
| 54 | + return true |
| 55 | + |
| 56 | + if self.get(0, 2) == self.get(1, 1) and self.get(0, 2) == self.get(2, 0) and self.get(0, 2) == player_id: |
| 57 | + return true |
| 58 | + |
| 59 | + return false |
| 60 | + |
| 61 | + fun current_player() -> Int: |
| 62 | + return int(self.playerTurn) + 1 |
| 63 | + |
| 64 | + fun next_turn(): |
| 65 | + self.playerTurn = !self.playerTurn |
| 66 | + |
| 67 | +# tic tac toe implementation |
| 68 | + |
| 69 | +@classes |
| 70 | +act play() -> Game: |
| 71 | + frm board : Board |
| 72 | + frm score = 10 |
| 73 | + while !board.full(): |
| 74 | + # sets the indicated board as beloning |
| 75 | + # to the current player |
| 76 | + act mark(BInt<0, 3> x, BInt<0, 3> y) { board.get(x.value, y.value) == 0 } |
| 77 | + |
| 78 | + score = score - 1 |
| 79 | + board.set(x.value, y.value, board.current_player()) |
| 80 | + |
| 81 | + if board.three_in_a_line_player(board.current_player()): |
| 82 | + return |
| 83 | + board.next_turn() |
| 84 | + |
| 85 | +fun get_current_player(Game g) -> Int: |
| 86 | + return int(g.board.playerTurn) |
| 87 | + |
| 88 | +fun score(Game g, Int player_id) -> Float: |
| 89 | + if !g.is_done(): |
| 90 | + return 0.0 |
| 91 | + if g.board.three_in_a_line_player(player_id + 1): |
| 92 | + return 1.0 |
| 93 | + else if g.board.three_in_a_line_player(((player_id + 1) % 2) + 1): |
| 94 | + return -1.0 |
| 95 | + |
| 96 | + return 0.0 |
| 97 | + |
| 98 | +fun get_num_players() -> Int: |
| 99 | + return 2 |
| 100 | + |
| 101 | +fun fuzz(Vector<Byte> input): |
| 102 | + if input.size() == 0: |
| 103 | + return |
| 104 | + let state = play() |
| 105 | + let action : AnyGameAction |
| 106 | + parse_and_execute(state, action, input) |
| 107 | + |
| 108 | +fun main() -> Int: |
| 109 | + let game = play() |
| 110 | + let x : BInt<0, 3> |
| 111 | + let y : BInt<0, 3> |
| 112 | + x.value = 0 |
| 113 | + y.value = 0 |
| 114 | + game.mark(x, y) |
| 115 | + if game.board.full(): |
| 116 | + return 1 |
| 117 | + x.value = 1 |
| 118 | + y.value = 0 |
| 119 | + game.mark(x, y) |
| 120 | + if game.board.full(): |
| 121 | + return 2 |
| 122 | + x.value = 1 |
| 123 | + y.value = 1 |
| 124 | + game.mark(x, y) |
| 125 | + if game.board.full(): |
| 126 | + return 3 |
| 127 | + x.value = 2 |
| 128 | + y.value = 0 |
| 129 | + game.mark(x, y) |
| 130 | + if game.board.full(): |
| 131 | + return 4 |
| 132 | + x.value = 2 |
| 133 | + y.value = 2 |
| 134 | + game.mark(x, y) |
| 135 | + if game.board.full(): |
| 136 | + return 5 |
| 137 | + if game.board.three_in_a_line_player(1): |
| 138 | + return 0 |
| 139 | + else: |
| 140 | + return 1 |
| 141 | + |
| 142 | +fun pretty_print(Game g): |
| 143 | + let i = 0 |
| 144 | + while i != 3: |
| 145 | + let to_print : String |
| 146 | + let y = 0 |
| 147 | + while y != 3: |
| 148 | + to_print.append(to_string(g.board.get(i, y))) |
| 149 | + y = y + 1 |
| 150 | + print(to_print) |
| 151 | + i = i + 1 |
| 152 | +#--- to_run.rb |
| 153 | +require_relative 'library' |
| 154 | +action = RLC::RLCAnyGameAction.new |
| 155 | +_actions = RLC::enumerate(action) |
| 156 | +print(_actions) |
| 157 | +actions = (0..._actions.size).map { |i| _actions.get(i) } |
| 158 | +action = actions[0] |
| 159 | +puts(action[0]) |
0 commit comments