TicTacToe/game.🗿

205 lines
3.6 KiB
Plaintext
Raw Permalink Normal View History

2023-06-20 15:30:28 -04:00
define INPUT = 0xbffe;
define SCREEN = 0xbfff;
define SPAWN_X = 1;
define SPAWN_Y = 1;
define CASE_SIZE = 42;
2023-06-20 17:03:35 -04:00
global player_turn = 0;
2023-06-20 15:30:28 -04:00
draw_pixel(x, y, c)
{
[SCREEN + (x & 127) + (y & 127) * 128] = c;
}
draw_x(x, y, selected)
{
local current_x = 0;
local current_y;
loop
{
if (current_x == CASE_SIZE)
return;
current_y = 0;
loop
{
if (current_y == CASE_SIZE)
break;
if (current_x == current_y | current_x == CASE_SIZE - current_y)
draw_pixel(x + current_x, y + current_y, 0xffff);
else
draw_pixel(x + current_x, y + current_y, 0x1111 * (selected + 2));
current_y++;
}
current_x++;
}
}
draw_o(x, y, selected)
{
local current_x = 0;
local current_y;
loop
{
if (current_x == CASE_SIZE)
return;
current_y = 0;
loop
{
if (current_y == CASE_SIZE)
break;
if (current_x == 2 | current_y == 2 | current_x == CASE_SIZE - 2 | current_y == CASE_SIZE - 2)
draw_pixel(x + current_x, y + current_y, 0xffff);
else
draw_pixel(x + current_x, y + current_y, 0x1111 * (selected + 2));
current_y++;
}
current_x++;
}
}
draw_blank(x, y, selected)
{
local current_x = 0;
local current_y;
loop
{
if (current_x == CASE_SIZE)
return;
current_y = 0;
loop
{
if (current_y == CASE_SIZE)
break;
draw_pixel(x + current_x, y + current_y, 0x1111 * (selected + 2));
current_y++;
}
current_x++;
}
}
draw_case(x, y, content, selected)
{
if (content == 'x')
draw_x(x, y, selected);
else if (content == 'o')
draw_o(x, y, selected);
else
draw_blank(x, y, selected);
}
draw_map(map, cursor_x, cursor_y)
{
local x = 0;
local y;
loop
{
if (x == 3)
return;
y = 0;
loop
{
if (y == 3)
break;
2023-06-20 15:47:05 -04:00
draw_case(x * CASE_SIZE + x, y * CASE_SIZE + y, [map + x * 3 + y], cursor_x == x & cursor_y == y);
2023-06-20 15:30:28 -04:00
y++;
}
x++;
}
}
2023-06-20 15:40:16 -04:00
global input_left;
global input_right;
global input_up;
global input_down;
2023-06-20 17:03:35 -04:00
global input_space;
2023-06-20 15:40:16 -04:00
input_update() {
local input = [INPUT];
input_up = (input & 0x10) != 0;
input_down = (input & 0x20) != 0;
input_left = (input & 0x40) != 0;
input_right = (input & 0x80) != 0;
2023-06-20 17:03:35 -04:00
input_space = (input & 0x1) != 0;
2023-06-20 15:40:16 -04:00
}
cursor_update(cursor_x_ptr, cursor_y_ptr)
{
if (input_up)
[cursor_y_ptr] = ([cursor_y_ptr] - 1) % 3;
if (input_down)
[cursor_y_ptr] = ([cursor_y_ptr] + 1) % 3;
if (input_left)
[cursor_x_ptr] = ([cursor_x_ptr] - 1) % 3;
if (input_right)
[cursor_x_ptr] = ([cursor_x_ptr] + 1) % 3;
}
2023-06-20 15:30:28 -04:00
2023-06-20 17:31:05 -04:00
check_victory(map, new_block_x, new_block_y)
{
local row = 1;
loop
{
if ([map + ((new_block_x + row) % 3) * 3 + new_block_y] != [map + new_block_x * 3 + new_block_y])
break;
if (row == 2)
return (1);
row++;
}
row = 1;
loop
{
if ([map + new_block_x * 3 + ((new_block_y + row) % 3)] != [map + new_block_x * 3 + new_block_y])
break;
if (row == 2)
return (1);
row++;
}
row = 1;
loop
{
if ([map + ((new_block_x + row) % 3) * 3 + ((new_block_y + row) % 3)] != [map + new_block_x * 3 + new_block_y])
break;
if (row == 2)
return (1);
row++;
}
return (0);
}
2023-06-20 17:03:35 -04:00
map_update(map, cursor_x, cursor_y)
{
2023-06-20 17:31:05 -04:00
if (input_space == 0 | [map + cursor_x * 3 + cursor_y] != 'b')
2023-06-20 17:03:35 -04:00
return;
if (player_turn)
[map + cursor_x * 3 + cursor_y] = 'x';
else
[map + cursor_x * 3 + cursor_y] = 'o';
player_turn = player_turn == 0;
}
2023-06-20 15:30:28 -04:00
main()
{
2023-06-20 15:40:16 -04:00
local cursor_x, cursor_y;
2023-06-20 15:30:28 -04:00
local map = {'b', 'b', 'b','b', 'b', 'b', 'b', 'b', 'b'};
2023-06-20 17:31:05 -04:00
2023-06-20 15:40:16 -04:00
cursor_x = SPAWN_X;
cursor_y = SPAWN_Y;
2023-06-20 15:30:28 -04:00
loop
{
2023-06-20 15:49:25 -04:00
slp;
slp;
2023-06-20 15:30:28 -04:00
slp;
2023-06-20 15:40:16 -04:00
draw_map(map, cursor_x, cursor_y);
input_update();
cursor_update(&cursor_x, &cursor_y);
2023-06-20 17:31:05 -04:00
if (input_space)
{
map_update(map, cursor_x, cursor_y);
if (check_victory(map, cursor_x, cursor_y))
break;
}
2023-06-20 15:30:28 -04:00
}
}