diff --git a/TODO b/TODO index fcde901..585b4cf 100644 --- a/TODO +++ b/TODO @@ -1,3 +1,4 @@ TODO for cttt 1 ai 1 player +2 make it interaktive diff --git a/logic.c b/logic.c index f163d83..311122c 100644 --- a/logic.c +++ b/logic.c @@ -1,6 +1,7 @@ //all the logical parts of the game #include #include +#include void clean_table(int *table){ int n; @@ -31,10 +32,12 @@ void place(int *table, int turn, int *xs, int *os){ int input; while(1 == 1){ - printf("Place marker on: "); - scanf("%i", &input); + printw("Place marker on: "); + refresh(); + scanw("%i", &input); - printf("input: %d\n", input); + printw("input: %d\n", input); + refresh(); if (table[input - 1] == 0){ table[input - 1] = turn; @@ -50,10 +53,12 @@ void take(int *table, int turn, int *xs, int *os){ int input; while(1 == 1){ - printf("take marker from: "); - scanf("%i", &input); + printw("take marker from: "); + refresh(); + scanw("%i", &input); - printf("input: %d\n", input); + printw("input: %d\n", input); + refresh(); if (table[input - 1] == turn){ table[input - 1] = 0; @@ -68,7 +73,7 @@ void take(int *table, int turn, int *xs, int *os){ void inputfunc(int *table, int turn, int *xs, int *os){ - printf("#x: %d #o: %d\n", *xs, *os); + printw("#x: %d #o: %d\n", *xs, *os); switch(turn){ case(1): diff --git a/main.c b/main.c index 6c96e7c..e5ee90e 100644 --- a/main.c +++ b/main.c @@ -1,28 +1,18 @@ //main file #include #include +#include //functions in logic.c void clean_table(int *table); int test_win(int *table, int turn); void inputfunc(int *table, int turn, int *xs, int *os); -//functions in table.c +//functions in visual.c void print_table(int *table); void print_turn(int turn); - -void win_message(int turn){ - char winner; - - if (turn == 1){winner = 'x';} - if (turn == 2){winner = 'o';} - - printf("#######################\n"); - printf("# #\n"); - printf("# %c Won! #\n", winner); - printf("# #\n"); - printf("#######################\n"); -} +void win_message(int turn); +int menu(); int two_player_loop(){ @@ -42,9 +32,8 @@ int two_player_loop(){ while(win == 0){ + clear(); print_turn(turn); - printf("interger turn:%d\n", turn); - printf("#x: %d #o: %d\n", *xs, *os); print_table(table); inputfunc(table, turn, xs, os); win = test_win(table, turn); @@ -63,43 +52,19 @@ int two_player_loop(){ } int one_player_loop(){ - printf("###################\n"); - printf("# Under #\n"); - printf("# construction! #\n"); - printf("###################\n"); + move(0, 0); + printw("###################\n"); + printw("# Under #\n"); + printw("# construction! #\n"); + printw("###################\n"); + refresh(); + getch(); } -int menu(){ - int * shoice = (int *) malloc(sizeof(int)); - - printf("###################\n"); - printf("# 1 - two player #\n"); - printf("# 2 - one player #\n"); - printf("# 3 - quit #\n"); - printf("###################\n"); - printf("Your shoice: "); - scanf("%d", shoice); - - switch(*shoice){ - case 1: - free(shoice); - two_player_loop(); - break; - case 2: - free(shoice); - one_player_loop(); - break; - case 3: - free(shoice); - return 0; - break; - } - - menu(); -} int main(){ - printf("Hello and wealcome to tic tac toe writhen in c.\n"); + initscr(); menu(); + endwin(); return 0; } diff --git a/makefile b/makefile index e9011ed..86e5ff3 100644 --- a/makefile +++ b/makefile @@ -1,2 +1,2 @@ -cttt: main.c logic.c table.c - gcc main.c logic.c table.c -fhosted -o cttt +cttt: main.c logic.c visual.c + gcc main.c logic.c visual.c -fhosted -lncurses -o cttt diff --git a/table.c b/table.c deleted file mode 100644 index 7efd05b..0000000 --- a/table.c +++ /dev/null @@ -1,110 +0,0 @@ -//this is the visual table part of the game -#include - -void clear_chartable(char * chartable[9][5]){ - - int x; - int z; - - for (int x = 0; x < 9; x++){ - for (int z = 0; z < 5; z++){ - chartable[x][z] = " "; - } - } - - chartable[0][2] = " 1 "; - chartable[1][2] = " 2 "; - chartable[2][2] = " 3 "; - chartable[3][2] = " 4 "; - chartable[4][2] = " 5 "; - chartable[5][2] = " 6 "; - chartable[6][2] = " 7 "; - chartable[7][2] = " 8 "; - chartable[8][2] = " 9 "; - - return; -} - -void table_to_chartable(int *table, char * chartable[9][5]){ - int x; - int z; - int n; - - char * cx[5]; - char * co[5]; - - cx[0] = "X X"; - cx[1] = " X X "; - cx[2] = " X "; - cx[3] = " X X "; - cx[4] = "X X"; - - co[0] = " OOO "; - co[1] = "O O"; - co[2] = "O O"; - co[3] = "O O"; - co[4] = " OOO "; - - for (x = 0; x < 9; x++){ - if (table[x] == 1){ - for (n = 0; n < 5; n++){ - chartable[x][n] = cx[n]; - } - } - if (table[x] == 2){ - for (n = 0; n < 5; n++){ - chartable[x][n] = co[n]; - } - } - } - return; -} - -void print_only(char * chartable[9][5]){ - printf("#########################\n"); - printf("# # # #\n"); - printf("# %s # %s # %s #\n", chartable[0][0], chartable[1][0], chartable[2][0]); - printf("# %s # %s # %s #\n", chartable[0][1], chartable[1][1], chartable[2][1]); - printf("# %s # %s # %s #\n", chartable[0][2], chartable[1][2], chartable[2][2]); - printf("# %s # %s # %s #\n", chartable[0][3], chartable[1][3], chartable[2][3]); - printf("# %s # %s # %s #\n", chartable[0][4], chartable[1][4], chartable[2][4]); - printf("# # # #\n"); - printf("#########################\n"); - printf("# # # #\n"); - printf("# %s # %s # %s #\n", chartable[3][0], chartable[4][0], chartable[5][0]); - printf("# %s # %s # %s #\n", chartable[3][1], chartable[4][1], chartable[5][1]); - printf("# %s # %s # %s #\n", chartable[3][2], chartable[4][2], chartable[5][2]); - printf("# %s # %s # %s #\n", chartable[3][3], chartable[4][3], chartable[5][3]); - printf("# %s # %s # %s #\n", chartable[3][4], chartable[4][4], chartable[5][4]); - printf("# # # #\n"); - printf("#########################\n"); - printf("# # # #\n"); - printf("# %s # %s # %s #\n", chartable[6][0], chartable[7][0], chartable[8][0]); - printf("# %s # %s # %s #\n", chartable[6][1], chartable[7][1], chartable[8][1]); - printf("# %s # %s # %s #\n", chartable[6][2], chartable[7][2], chartable[8][2]); - printf("# %s # %s # %s #\n", chartable[6][3], chartable[7][3], chartable[8][3]); - printf("# %s # %s # %s #\n", chartable[6][4], chartable[7][4], chartable[8][4]); - printf("# # # #\n"); - printf("#########################\n"); - return; -} - -void print_table(int *table){ - char * chartable[9][5]; - clear_chartable(chartable); - table_to_chartable(table, chartable); - print_only(chartable); - return; -} - -void print_turn(int turn){ - switch(turn){ - case(1): - printf("X's turn\n"); - break; - case(2): - printf("O's turn\n"); - break; - } - return; -} diff --git a/visual.c b/visual.c new file mode 100644 index 0000000..c615fc8 --- /dev/null +++ b/visual.c @@ -0,0 +1,168 @@ +//this is the visual table part of the game +#include +#include +#include + +//funktions in main.c +int one_player_loop(); +int two_player_loop(); + +void win_message(int turn){ + char winner; + + if (turn == 1){winner = 'x';} + if (turn == 2){winner = 'o';} + + printw("#######################\n"); + printw("# #\n"); + printw("# %c Won! #\n", winner); + printw("# #\n"); + printw("#######################\n"); + getch(); + refresh(); +} + +int menu(){ + int * shoice = (int *) malloc(sizeof(int)); + int end = 0; + + while(end != 1){ + clear(); + move(0, 0); + printw("###################\n"); + printw("# 1 - two player #\n"); + printw("# 2 - one player #\n"); + printw("# 3 - quit #\n"); + printw("###################\n"); + printw("Your shoice: "); + refresh(); + scanw("%d", shoice); + + switch(*shoice){ + case 1: + free(shoice); + two_player_loop(); + break; + case 2: + free(shoice); + one_player_loop(); + break; + case 3: + free(shoice); + return 0; + break; + } + } + + menu(); +} + +void clear_chartable(char * chartable[9][5]){ + + int x; + int z; + + for (int x = 0; x < 9; x++){ + for (int z = 0; z < 5; z++){ + chartable[x][z] = " "; + } + } + + chartable[0][2] = " 1 "; + chartable[1][2] = " 2 "; + chartable[2][2] = " 3 "; + chartable[3][2] = " 4 "; + chartable[4][2] = " 5 "; + chartable[5][2] = " 6 "; + chartable[6][2] = " 7 "; + chartable[7][2] = " 8 "; + chartable[8][2] = " 9 "; + + return; +} + +void table_to_chartable(int *table, char * chartable[9][5]){ + int x; + int z; + int n; + + char * cx[5]; + char * co[5]; + + cx[0] = "X X"; + cx[1] = " X X "; + cx[2] = " X "; + cx[3] = " X X "; + cx[4] = "X X"; + + co[0] = " OOO "; + co[1] = "O O"; + co[2] = "O O"; + co[3] = "O O"; + co[4] = " OOO "; + + for (x = 0; x < 9; x++){ + if (table[x] == 1){ + for (n = 0; n < 5; n++){ + chartable[x][n] = cx[n]; + } + } + if (table[x] == 2){ + for (n = 0; n < 5; n++){ + chartable[x][n] = co[n]; + } + } + } + return; +} + +void print_only(char * chartable[9][5]){ + move(0, 0); + printw("#########################\n"); + printw("# # # #\n"); + printw("# %s # %s # %s #\n", chartable[0][0], chartable[1][0], chartable[2][0]); + printw("# %s # %s # %s #\n", chartable[0][1], chartable[1][1], chartable[2][1]); + printw("# %s # %s # %s #\n", chartable[0][2], chartable[1][2], chartable[2][2]); + printw("# %s # %s # %s #\n", chartable[0][3], chartable[1][3], chartable[2][3]); + printw("# %s # %s # %s #\n", chartable[0][4], chartable[1][4], chartable[2][4]); + printw("# # # #\n"); + printw("#########################\n"); + printw("# # # #\n"); + printw("# %s # %s # %s #\n", chartable[3][0], chartable[4][0], chartable[5][0]); + printw("# %s # %s # %s #\n", chartable[3][1], chartable[4][1], chartable[5][1]); + printw("# %s # %s # %s #\n", chartable[3][2], chartable[4][2], chartable[5][2]); + printw("# %s # %s # %s #\n", chartable[3][3], chartable[4][3], chartable[5][3]); + printw("# %s # %s # %s #\n", chartable[3][4], chartable[4][4], chartable[5][4]); + printw("# # # #\n"); + printw("#########################\n"); + printw("# # # #\n"); + printw("# %s # %s # %s #\n", chartable[6][0], chartable[7][0], chartable[8][0]); + printw("# %s # %s # %s #\n", chartable[6][1], chartable[7][1], chartable[8][1]); + printw("# %s # %s # %s #\n", chartable[6][2], chartable[7][2], chartable[8][2]); + printw("# %s # %s # %s #\n", chartable[6][3], chartable[7][3], chartable[8][3]); + printw("# %s # %s # %s #\n", chartable[6][4], chartable[7][4], chartable[8][4]); + printw("# # # #\n"); + printw("#########################\n"); + refresh(); + return; +} + +void print_table(int *table){ + char * chartable[9][5]; + clear_chartable(chartable); + table_to_chartable(table, chartable); + print_only(chartable); + return; +} + +void print_turn(int turn){ + switch(turn){ + case(1): + printw("X's turn\n"); + break; + case(2): + printw("O's turn\n"); + break; + } + return; +}