Created
December 30, 2019 23:52
-
-
Save ezvezdov/cf7e6ed770a8a2e369748c9873d8bfbb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
VOID = '_' | |
HUMAN_SIGN = 'x' | |
BOT_SIGN = 'o' | |
DRAW_SIGN = 'N' | |
BOARD_LENGTH = 3 | |
board = [['_','_','_'],['_','_','_'],['_','_','_']] | |
bot_board = [[3, 2, 3], [2, 4, 2], [3, 2, 3]] | |
function start() | |
println("·singleplayer - type 1") | |
println("·multiplayer - type 2") | |
choose_mod = readline() | |
if choose_mod == "1" | |
players = 1 | |
println("·Your sign:", HUMAN_SIGN) | |
println("·Computer's sign:", BOT_SIGN) | |
end | |
if choose_mod == "2" | |
players = 2 | |
println("·1st player's sign:", HUMAN_SIGN) | |
println("·2nd player's sing:", BOT_SIGN) | |
end | |
println("·Input Example: B2") | |
print_board() | |
human_step(players) | |
end | |
function input_coord() | |
i = 4 | |
j = 4 | |
while i > 3 || j > 3 || board[i][j] != VOID | |
print("Position:") | |
input_str = readline() | |
println() | |
i = Int(input_str[1]) - Int('A') + 1 | |
j = Int(input_str[2]) - Int('0') | |
end | |
return i, j | |
end | |
function human2_step() | |
i,j = input_coord() | |
board[i][j] = BOT_SIGN | |
print_board() | |
check_game_over(2) #players | |
human_step(2) #players | |
end | |
function human_step(players) | |
i,j = input_coord() | |
board[i][j] = HUMAN_SIGN | |
print_board() | |
check_game_over(players) | |
if players == 1 | |
bot_analize(i, j) | |
else | |
human2_step() | |
end | |
end | |
function print_board() | |
println(" 1 2 3 ") | |
for i = 1:BOARD_LENGTH | |
print('A' + i - 1,' ') | |
for j = 1:BOARD_LENGTH | |
print(board[i][j],' ') | |
end | |
println() | |
end | |
println("_________") | |
end | |
function horizontal(k, char) | |
n = 0 | |
lst = [-1] | |
for i = 1 : BOARD_LENGTH | |
if board[k][i] == char | |
n += 1 | |
end | |
if board[k][i] == VOID | |
lst = [k, i] | |
end | |
end | |
return n, lst | |
end | |
function vertical(k, char) | |
n = 0 | |
lst = [-1] | |
for i = 1:BOARD_LENGTH | |
if board[i][k] == char | |
n += 1 | |
end | |
if board[i][k] == VOID | |
lst = [i, k] | |
end | |
end | |
return n, lst | |
end | |
function diagonal(diagonal_type, char) # 1- from vverhni corner | |
n = 0 # 3 - from left down corner | |
lst = [-1] | |
j = 3 | |
for i = 1:BOARD_LENGTH | |
if diagonal_type == 1 | |
j = i | |
end | |
if board[i][j] == char | |
n += 1 | |
end | |
if board[i][j] == VOID | |
lst = [i, j] | |
end | |
j -= 1 | |
end | |
return n, lst | |
end | |
function check(n, lst,char) | |
if n == 2 | |
return [lst] | |
end | |
if n == BOARD_LENGTH | |
game_over(char,1) | |
end | |
return [] | |
end | |
function bot_analize(h_i, h_j) # human i and human j | |
danger_coordinates = [] | |
maximum_coordinates =[] | |
win_checks = [] | |
# It's first part of analize, checking | |
# dangerous checks for bot and win checks | |
# for bot | |
for i = 1:BOARD_LENGTH | |
n, lst = horizontal(i, HUMAN_SIGN) | |
append!(danger_coordinates,check(n, lst, HUMAN_SIGN)) | |
n, lst = vertical(i, HUMAN_SIGN) | |
append!(danger_coordinates,check(n, lst, HUMAN_SIGN)) | |
n, lst = horizontal(i, BOT_SIGN) | |
append!(win_checks,check(n, lst, BOT_SIGN)) | |
n, lst = vertical(i, BOT_SIGN) | |
append!(win_checks,check(n, lst, BOT_SIGN)) | |
end | |
for j = 2:BOARD_LENGTH | |
n, lst = diagonal(j, HUMAN_SIGN) | |
append!(danger_coordinates,check(n, lst, HUMAN_SIGN)) | |
n, lst = diagonal(j, BOT_SIGN) | |
append!(win_checks,check(n, lst, BOT_SIGN)) | |
end | |
######################################### | |
bot_board[h_i][h_j] = 0 | |
for i = 1:BOARD_LENGTH | |
if bot_board[h_i][i] != 0 | |
bot_board[h_i][i] -= 1 | |
end | |
if bot_board[i][h_i] != 0 | |
bot_board[i][h_i] -= 1 | |
end | |
end | |
if h_i + h_j == 3 | |
j = 3 | |
for i = 1:BOARD_LENGTH | |
bot_board[i][j] -= 1 | |
j -= 1 | |
end | |
end | |
if h_i == h_j | |
for i = 1:BOARD_LENGTH | |
bot_board[i][i] -= 1 | |
end | |
end | |
maximum = 0 | |
for i = 1:BOARD_LENGTH | |
for j = 1:BOARD_LENGTH | |
if bot_board[i][j] > maximum | |
maximum = bot_board[i][j] | |
maximum_coordinates = [] | |
append!(maximum_coordinates,[[i,j]]) | |
end | |
if bot_board[i][j] == maximum | |
append!(maximum_coordinates,[[i,j]]) | |
end | |
end | |
end | |
x = -1 | |
y = -1 | |
if x == -1 && y == -1 | |
############################### | |
i = rand(1:length(maximum_coordinates)) | |
while maximum_coordinates[i] == [-1] || maximum_coordinates[i] == [] | |
i = rand(1:length(maximum_coordinates)) | |
end | |
x = maximum_coordinates[i][1] | |
y = maximum_coordinates[i][2] | |
end | |
if board[2][2] == VOID | |
x = 2 | |
y = 2 | |
end | |
for i = 1:length(danger_coordinates) | |
if danger_coordinates[i] != [-1] && danger_coordinates[i] != [] | |
x = danger_coordinates[i][1] | |
y = danger_coordinates[i][2] | |
end | |
end | |
for i = 1:length(win_checks) | |
if win_checks[i] != [-1] && win_checks[i] != [] | |
x = win_checks[i][1] | |
y = win_checks[i][2] | |
board[x][y] = BOT_SIGN | |
print_board() | |
game_over(BOT_SIGN,1) | |
end | |
end | |
bot_step(x, y) | |
end | |
function bot_step(i, j) | |
# x = i, j = y | |
board[i][j] = BOT_SIGN | |
bot_board[i][j] = 0 | |
print_board() | |
human_step(1) | |
end | |
function check_game_over(players) | |
if(board[1][1] == board[1][2] == board[1][3] != VOID|| board[1][1] == board[2][1] == board[3][1] != VOID || board[1][1] == board[2][2] == board[3][3] != VOID) | |
game_over(board[1][1],players) | |
end | |
if(board[2][1] == board[2][2] == board[2][3] != VOID || board[1][2] == board[2][2] == board[3][2] != VOID) | |
game_over(board[2][2],players) | |
end | |
if(board[3][1] == board[3][2] == board[3][3] != VOID || board[1][3] == board[2][3] == board[3][3] != VOID) | |
game_over(board[3][3],players) | |
end | |
if(board[1][3] == board[2][1] == board[3][1] != VOID) | |
game_over(board[1][3],players) | |
end | |
if !(VOID in board[1]) && !(VOID in board[2]) && !(VOID in board[3]) | |
game_over(DRAW_SIGN,players) | |
end | |
end | |
function game_over(char,players) | |
if char == HUMAN_SIGN | |
println("·1st player win!") | |
end | |
if char == BOT_SIGN | |
if players == 1 | |
println("·You lose :(") | |
elseif players == 2 | |
println("·2nd player win!") | |
end | |
end | |
if char == DRAW_SIGN | |
println("·Draw") | |
end | |
exit(0) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice work! There are definitely some efficiency improvements needed but it looks great.