Skip to content

Commit af2fea6

Browse files
committed
Added tic tac toe
1 parent eda8ab7 commit af2fea6

File tree

3 files changed

+128
-5
lines changed

3 files changed

+128
-5
lines changed

Makefile

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ app_entry_o = $(BUILD_DIR)/app_entry.o
1717

1818
# Apps
1919
app_calc = $(BUILD_DIR)/calc
20+
app_ttt = $(BUILD_DIR)/ttt
2021
app_dashboard = $(BUILD_DIR)/dashboard
2122

2223
# Parameters
@@ -28,21 +29,25 @@ all: images binaries
2829

2930
images: $(image_vmdk)
3031

31-
binaries: $(bt_stage1) $(bt_stage2) $(app_calc)
32+
binaries: $(bt_stage1) $(bt_stage2)
3233

33-
$(image_vmdk): $(bt_stage1) $(bt_stage2) $(app_calc)
34+
$(image_vmdk): $(bt_stage1) $(bt_stage2) $(app_calc) $(app_ttt)
3435
dd bs=512 count=2 if=$(bt_stage1) of=$@
3536
/bin/echo -ne "\x55\xaa" | dd seek=510 bs=1 of=$@
3637
cat $(bt_stage2) >> $@
3738
cat $(app_calc) >> $@
39+
cat $(app_ttt) >> $@
3840
@echo "Stage 1 Size : " $$(stat -c %s $(bt_stage1))
3941
@echo "Stage 2 Size : " $$(stat -c %s $(bt_stage2))
4042
@echo "App Calc Size : " $$(stat -c %s $(app_calc))
43+
@echo "App TTT Size : " $$(stat -c %s $(app_ttt))
4144
@echo "Image Size : " $$(stat -c %s $@)
4245
@echo "Want BT_STAGE2_SECTOR_COUNT : 0x"$$(printf "%x\n" $$(( $$(stat -c %s $(bt_stage2)) / 512)) )
4346
@echo "Got BT_STAGE2_SECTOR_COUNT : 0x"$(BT_STAGE2_SECTOR_COUNT)
44-
@echo "AppSector Sector Count : "$$(( $$(stat -c %s $(app_calc)) / 512))
45-
@echo "AppSector Sector Start : "$$(( 1 + $$(stat -c %s $(bt_stage1)) / 512 + $$(stat -c %s $(bt_stage2)) / 512))
47+
@echo "AppCalc Sector Count : "$$(( $$(stat -c %s $(app_calc)) / 512))
48+
@echo "AppCalc Sector Start : "$$(( 1 + $$(stat -c %s $(bt_stage1)) / 512 + $$(stat -c %s $(bt_stage2)) / 512))
49+
@echo "AppTTT Sector Count : "$$(( $$(stat -c %s $(app_ttt)) / 512))
50+
@echo "AppTTT Sector Start : "$$(( 1 + $$(stat -c %s $(bt_stage1)) / 512 + $$(stat -c %s $(bt_stage2)) / 512 + $$(stat -c %s $(app_calc)) / 512))
4651

4752
$(bt_stage1): $(BL_SRC_DIR)/stage1.asm $(BL_SRC_DIR)/constants.asm $(BL_SRC_DIR)/io.asm $(BL_SRC_DIR)/disk.asm
4853
nasm -o $@ -f bin -i $(BL_SRC_DIR)/ -D BT_STAGE2_SECTOR_COUNT=$(BT_STAGE2_SECTOR_COUNT) $<
@@ -66,6 +71,11 @@ $(app_calc): $(app_entry_o) $(APP_DIR)/calc.c $(SYSCALLS_SRC_DIR)/basic.h $(SYSC
6671
ld --oformat binary -m elf_i386 -Ttext 0xC000 --strip-all -o $@ $(app_entry_o) $(BUILD_DIR)/calc.o
6772
truncate --size=%512 $@
6873

74+
$(app_ttt): $(app_entry_o) $(APP_DIR)/tic_tac_toe.c $(SYSCALLS_SRC_DIR)/basic.h $(SYSCALLS_SRC_DIR)/io.h $(SYSCALLS_SRC_DIR)/time.h $(SYSCALLS_SRC_DIR)/color.h $(UTIL_SRC_DIR)/string.h $(BL_SRC_DIR)/constants.asm $(BL_SRC_DIR)/io.asm $(SYSCALLS_SRC_DIR)/io_syscall.asm $(SYSCALLS_SRC_DIR)/time_syscall.asm
75+
gcc -m16 -fno-pie -c -Isrc -o $(BUILD_DIR)/ttt.o $(APP_DIR)/tic_tac_toe.c
76+
ld --oformat binary -m elf_i386 -Ttext 0xC000 --strip-all -o $@ $(app_entry_o) $(BUILD_DIR)/ttt.o
77+
truncate --size=%512 $@
78+
6979
debug_stage1: $(bt_stage1)
7080
objdump -b binary -mi386 -Maddr16,data16 -D $<
7181
xxd $<

src/app/dashboard.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ int run_dashboard(int argc,char **argv) {
4646
int num = read_int();
4747

4848
// Load and launch calculator
49-
int err = load_sectors(0xC000, 0x80, 27, 25);
49+
// int err = load_sectors(0xC000, 0x80, 27, 25);
50+
int err = load_sectors(0xC000, 0x80, 52, 25);
5051
if(err) {
5152
set_color_bg(C_DARK_GRAY);
5253
move_xy(2,WINDOW_HEIGHT);

src/app/tic_tac_toe.c

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
// Simple Calculator
2+
#define BUILD_FOR_FUZZY
3+
#ifndef BUILD_FOR_FUZZY
4+
#else
5+
#include <lib/syscalls/io.h>
6+
#include <lib/syscalls/time.h>
7+
#include <lib/util/string.h>
8+
#endif
9+
10+
void console_init() {
11+
set_color_bg(C_BLACK);
12+
set_color_fg(C_WHITE);
13+
print_rectangle(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
14+
move_xy(0,0);
15+
}
16+
17+
struct State
18+
{
19+
unsigned char turn; // 0 or 1
20+
unsigned char mat[3][3];
21+
};
22+
23+
void reset(struct State *s) {
24+
s->turn = 0;
25+
for (int i = 0; i < 3; ++i)
26+
for (int j = 0; j < 3; ++j)
27+
s->mat[i][j]=' ';
28+
}
29+
30+
void print_board(struct State *s) {
31+
for (int i = 0; i < 3; ++i) {
32+
print_line(" ");
33+
print_char(s->mat[i][0]);
34+
print_char('|');
35+
print_char(s->mat[i][1]);
36+
print_char('|');
37+
print_char(s->mat[i][2]);
38+
print_char('\n');
39+
if(i<2) {
40+
print_line(" -----\n");
41+
}
42+
}
43+
}
44+
45+
void redraw(struct State *s) {
46+
console_init();
47+
print_line("TicTacToe\n");
48+
print_line("---------\n");
49+
print_line("\n");
50+
print_line("Player 1 : X\n");
51+
print_line("Player 2 : O\n");
52+
print_line("\n");
53+
print_line("Turn: Player ");
54+
print_int(s->turn+1);
55+
print_line("\n");
56+
print_line("Controls: Use 1-9 in numpad keys pattern\n");
57+
print_line(" : R to reset game\n");
58+
print_line(" : Q to quit game\n\n");
59+
print_board(s);
60+
}
61+
62+
void play_move(struct State *s, unsigned char r, unsigned char c) {
63+
if(s->mat[r][c]!=' ')
64+
return;
65+
char mark = 'X';
66+
if (s->turn==1) {
67+
mark = 'O';
68+
}
69+
s->mat[r][c]=mark;
70+
s->turn=1-(s->turn);
71+
}
72+
73+
struct State s;
74+
unsigned char quit;
75+
unsigned char greset;
76+
int main(int argc,char *argv[]) {
77+
greset = 1;
78+
quit = 0;
79+
while(1) {
80+
if(quit) break;
81+
if(greset) {
82+
reset(&s);
83+
greset = 0;
84+
}
85+
redraw(&s);
86+
while(1){
87+
char c = getch();
88+
if(c=='r' || c=='R') {
89+
greset = 1;
90+
break;
91+
}
92+
if(c=='q' || c=='Q') {
93+
quit = 1;
94+
break;
95+
}
96+
char row = -1, col = -1;
97+
if(c=='1' || c=='2' || c=='3') row = 2;
98+
if(c=='4' || c=='5' || c=='6') row = 1;
99+
if(c=='7' || c=='8' || c=='9') row = 0;
100+
if(c=='1' || c=='4' || c=='7') col = 0;
101+
if(c=='2' || c=='5' || c=='8') col = 1;
102+
if(c=='3' || c=='6' || c=='9') col = 2;
103+
if(row>=0) {
104+
play_move(&s, row, col);
105+
break;
106+
} else {
107+
print_int(c);
108+
}
109+
}
110+
}
111+
return 0;
112+
}

0 commit comments

Comments
 (0)