|
| 1 | +#!/usr/bin/env lua |
| 2 | + |
| 3 | +local function printBoard(board) |
| 4 | + io.write("\n---------\n") |
| 5 | + for i = 1, 9 do |
| 6 | + io.write(board[i] .. (i % 3 ~= 0 and " | " or "\n---------\n")) |
| 7 | + end |
| 8 | + print() |
| 9 | +end |
| 10 | + |
| 11 | +local function checkWin(board, player) |
| 12 | + if (board[5] == player) and ((board[1] == player and board[9] == player) or (board[3] == player and board[7] == player)) then |
| 13 | + return true |
| 14 | + end |
| 15 | + |
| 16 | + for i = 1, 7, 3 do |
| 17 | + if board[i] == player and board[i + 1] == player and board[i + 2] == player then |
| 18 | + return true |
| 19 | + end |
| 20 | + end |
| 21 | + |
| 22 | + for i = 1, 3 do |
| 23 | + if board[i] == player and board[i + 3] == player and board[i + 6] == player then |
| 24 | + return true |
| 25 | + end |
| 26 | + end |
| 27 | + |
| 28 | + return false |
| 29 | +end |
| 30 | + |
| 31 | +local function boardFull(board) |
| 32 | + for i = 1, 9 do |
| 33 | + if tonumber(board[i]) then |
| 34 | + return false |
| 35 | + end |
| 36 | + end |
| 37 | + return true |
| 38 | +end |
| 39 | + |
| 40 | +local function humanMove(board) |
| 41 | + while true do |
| 42 | + io.write("Enter a number: ") |
| 43 | + io.flush() |
| 44 | + local n = tonumber(io.read()) |
| 45 | + if n and n < 10 and n > 0 then |
| 46 | + local i |
| 47 | + if n < 4 then |
| 48 | + i = n + 6 |
| 49 | + elseif n > 6 then |
| 50 | + i = n - 6 |
| 51 | + else |
| 52 | + i = n |
| 53 | + end |
| 54 | + |
| 55 | + if tonumber(board[i]) then |
| 56 | + board[i] = "U" |
| 57 | + break |
| 58 | + end |
| 59 | + print(n .. " is taken by " .. board[i] .. ". Try another position...") |
| 60 | + else |
| 61 | + print("Invalid input! Try a number between 1 and 9") |
| 62 | + end |
| 63 | + end |
| 64 | +end |
| 65 | + |
| 66 | +local function computerMove(board) |
| 67 | + local function oppositeOf(i) |
| 68 | + return i == 1 and 9 |
| 69 | + or i == 3 and 7 |
| 70 | + or i == 7 and 3 |
| 71 | + or i == 9 and 1 |
| 72 | + or i == 2 and 8 |
| 73 | + or i == 4 and 6 |
| 74 | + or i == 6 and 4 |
| 75 | + or 2 |
| 76 | + end |
| 77 | + |
| 78 | + local function SelectAnything() |
| 79 | + for j = 1, 9 do |
| 80 | + if tonumber(board[j]) then |
| 81 | + print("Selecting " .. j .. " as a last resort") |
| 82 | + return j |
| 83 | + end |
| 84 | + end |
| 85 | + end |
| 86 | + |
| 87 | + local i |
| 88 | + |
| 89 | + if tonumber(board[5]) then |
| 90 | + i = 5 |
| 91 | + else |
| 92 | + local r, map, c = math.floor(math.random(1, 4)), { 1, 3, 7, 9 } |
| 93 | + c = board[map[r]] |
| 94 | + if tonumber(c) then |
| 95 | + i = map[r] |
| 96 | + elseif c ~= "C" and tonumber(board[oppositeOf(map[r])]) then |
| 97 | + i = oppositeOf(map[r]) |
| 98 | + else |
| 99 | + map = { 2, 4, 6, 8 } |
| 100 | + c = board[map[r]] |
| 101 | + if tonumber(c) then |
| 102 | + i = map[r] |
| 103 | + elseif c ~= "C" and tonumber(board[oppositeOf(map[r])]) then |
| 104 | + i = oppositeOf(map[r]) |
| 105 | + else |
| 106 | + i = SelectAnything() |
| 107 | + end |
| 108 | + end |
| 109 | + end |
| 110 | + |
| 111 | + print("Computer chooses: " .. board[i]) |
| 112 | + board[i] = "C" |
| 113 | +end |
| 114 | + |
| 115 | +-- Main game loop |
| 116 | +local function game() |
| 117 | + -- Initialize the board |
| 118 | + local b = { "7", "8", "9", "4", "5", "6", "1", "2", "3" } |
| 119 | + |
| 120 | + printBoard(b) |
| 121 | + |
| 122 | + while true do |
| 123 | + humanMove(b) |
| 124 | + |
| 125 | + if checkWin(b, "U") then |
| 126 | + print("Congratulations! You win!") |
| 127 | + printBoard(b) |
| 128 | + break |
| 129 | + elseif boardFull(b) then |
| 130 | + print("It's a tie!") |
| 131 | + printBoard(b) |
| 132 | + break |
| 133 | + end |
| 134 | + |
| 135 | + computerMove(b) |
| 136 | + |
| 137 | + if checkWin(b, "C") then |
| 138 | + print("Computer wins! Better luck next time.") |
| 139 | + printBoard(b) |
| 140 | + break |
| 141 | + elseif boardFull(b) then |
| 142 | + print("It's a tie!") |
| 143 | + printBoard(b) |
| 144 | + break |
| 145 | + end |
| 146 | + |
| 147 | + printBoard(b) |
| 148 | + end |
| 149 | +end |
| 150 | + |
| 151 | +while true do |
| 152 | + game() |
| 153 | + io.write("Would you like to play again? (y/N) ") |
| 154 | + io.flush() |
| 155 | + if string.upper(io.read()) ~= "Y" then |
| 156 | + break |
| 157 | + end |
| 158 | +end |
0 commit comments