Add model
This commit is contained in:
parent
90508a3b3b
commit
f33b6c8acb
6 changed files with 467 additions and 10 deletions
|
@ -7,11 +7,11 @@
|
|||
|
||||
CC=gcc
|
||||
CFLAGS=-Wall -g -std=c99
|
||||
CPPFLAGS=
|
||||
CPPFLAGS=-I../include
|
||||
|
||||
all: utictactoe
|
||||
|
||||
utictactoe: utictactoe.c ## Compile utictactoe.c
|
||||
utictactoe: utictactoe.c model.c## Compile utictactoe.c
|
||||
|
||||
# Nettoyage
|
||||
.PHONY: clean help
|
||||
|
|
221
src/model.c
Normal file
221
src/model.c
Normal file
|
@ -0,0 +1,221 @@
|
|||
#include "model.h"
|
||||
|
||||
|
||||
/*
|
||||
* Create empty move
|
||||
*/
|
||||
|
||||
s_move *create_empty_move(){
|
||||
s_move * value = (s_move*) malloc(sizeof(s_move));
|
||||
if ( value ) {
|
||||
value->inner_position = FREE;
|
||||
value->outer_position = FREE;
|
||||
value->player = NOBODY;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Create an empty tictactoe
|
||||
*/
|
||||
s_tictactoe *create_empty_tictactoe() {
|
||||
|
||||
// Create s_tictactoe structure
|
||||
s_tictactoe * value = (s_tictactoe*) malloc(sizeof(s_tictactoe));
|
||||
if ( value ) {
|
||||
// create player
|
||||
value->winner = NOBODY;
|
||||
value->content = (e_player*) malloc(sizeof(e_player) * TICTACTOE_SIZE);
|
||||
if ( ! value->content ) {
|
||||
return value->content;
|
||||
}
|
||||
for ( int i=0;i<TICTACTOE_SIZE; i++){
|
||||
value->content[i] = NOBODY;
|
||||
}
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
/*
|
||||
* Create empty uTicTacToe
|
||||
*/
|
||||
|
||||
s_utictactoe *create_empty_utictactoe (uint inception_level) {
|
||||
s_utictactoe * value = (s_utictactoe*) malloc(sizeof(s_utictactoe));
|
||||
if ( value ) {
|
||||
|
||||
// then initiate outer_tictactoe amd inception_level
|
||||
value->outer_tictactoe = create_empty_tictactoe();
|
||||
value->inception_level = inception_level;
|
||||
value->history = NULL; // no history for now
|
||||
|
||||
// initialize inner TTT
|
||||
if (inception_level == 1){
|
||||
value->inner_tictactoes = NULL;
|
||||
}
|
||||
else {
|
||||
// As inception level can't be more than 2 (for now), inner_ttts
|
||||
// should be 9 simple ttt
|
||||
|
||||
value->inner_tictactoes = (s_tictactoe**)
|
||||
malloc(sizeof(s_tictactoe));
|
||||
|
||||
if ( ! value->inner_tictactoes ) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
for ( int i=0; i<TICTACTOE_SIZE; i++) {
|
||||
value->inner_tictactoes[i] = create_empty_tictactoe();
|
||||
}
|
||||
}
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
|
||||
void free_move(s_move *p_move){
|
||||
if ( p_move == NULL ) {
|
||||
free(p_move);
|
||||
}
|
||||
}
|
||||
|
||||
void free_tictactoe(s_tictactoe *p_ttt){
|
||||
if ( p_ttt) {
|
||||
free(p_ttt->content);
|
||||
free(p_ttt);
|
||||
p_ttt = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
void free_utictactoe(s_utictactoe *p_uttt){
|
||||
if ( p_uttt ) {
|
||||
// if we have inner_ttt we need to remove inet
|
||||
if ( p_uttt->inner_tictactoes != NULL ) {
|
||||
for ( int i=0; i<TICTACTOE_SIZE; i++) {
|
||||
free_tictactoe(p_uttt->inner_tictactoes[i]);
|
||||
}
|
||||
free(p_uttt->inner_tictactoes);
|
||||
}
|
||||
free_tictactoe(p_uttt->outer_tictactoe);
|
||||
free(p_uttt);
|
||||
p_uttt = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
e_player get_next_player_to_play(s_utictactoe *p_uttt) {
|
||||
if ( p_uttt->history->last_move->player == PLAYER_O ) {
|
||||
return PLAYER_X;
|
||||
}
|
||||
else {
|
||||
return PLAYER_O;
|
||||
}
|
||||
}
|
||||
|
||||
e_location get_next_outer_position(s_utictactoe *p_uttt) {
|
||||
if ( p_uttt->inner_tictactoes != NULL ) {
|
||||
e_location pos = p_uttt->history->last_move->inner_position;
|
||||
if ( p_uttt->inner_tictactoes->content[pos]->winner == NOBODY ) {
|
||||
return pos;
|
||||
}
|
||||
}
|
||||
return FREE;
|
||||
}
|
||||
|
||||
e_status is_move_valid(s_utictactoe *p_uttt, s_move *p_move){
|
||||
if ( get_next_player_to_play(p_uttt) != p_move->player ) {
|
||||
return NO;
|
||||
}
|
||||
return YES;
|
||||
}
|
||||
|
||||
void set_tictactoe_winner(s_tictactoe *p_ttt){
|
||||
bool find;
|
||||
int next;
|
||||
|
||||
if ( p_ttt->winner == NOBODY ) {
|
||||
for(int i = 0; i < TICTACTOE_WIDTH; i++){
|
||||
|
||||
/*
|
||||
* Test for first diagonale
|
||||
*/
|
||||
if ( p_ttt->content[i] != NOBODY ) {
|
||||
// if case 0 we need to test diagonale
|
||||
if ( i == 0 ){
|
||||
int next;
|
||||
find = true;
|
||||
for (int l = 1; l < TICTACTOE_WIDTH; l++) {
|
||||
// Maybe it is overkill to test diagonal but...
|
||||
// ... no need to rewrite if we update TICTACTOE_WIDTH
|
||||
next = l * TICTACTOE_WIDTH + l;
|
||||
if ( p_ttt->content[i] != p_ttt ->content[next]) {
|
||||
find = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (find == true) {
|
||||
p_ttt->winner = p_ttt->content[i];
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* second diagonal, from the end of line
|
||||
*/
|
||||
if ( i == TICTACTOE_WIDTH - 1 ) {
|
||||
find = true;
|
||||
for ( int l = 1; l < TICTACTOE_WIDTH; l++){
|
||||
next = (TICTACTOE_WIDTH - 1) * l;
|
||||
if ( p_ttt->content[i] == p_ttt->content[next] ) {
|
||||
find = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if ( find == true ){
|
||||
p_ttt->winner = p_ttt->content[i];
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* column
|
||||
*/
|
||||
find = true;
|
||||
for (int c = 1; c < TICTACTOE_WIDTH; c++ ) {
|
||||
next = TICTACTOE_WIDTH * c + i;
|
||||
if ( p_ttt->content[i] != p_ttt->content[next] ) {
|
||||
find = false;
|
||||
break;
|
||||
}
|
||||
if ( find == true ){
|
||||
p_ttt->winner = p_ttt->content[i];
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* line
|
||||
*/
|
||||
int line = i * TICTACTOE_WIDTH;
|
||||
if (p_ttt->content[line] != NOBODY){
|
||||
find = true;
|
||||
for ( int l = 1; l < TICTACTOE_WIDTH; l++ ) {
|
||||
next = line + l;
|
||||
if ( p_ttt->content[line] != p_ttt->content[next] ) {
|
||||
find = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if ( find == true ) {
|
||||
p_ttt->winner = p_ttt->content[line];
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
e_status play_move(s_utictactoe *p_ttt, s_move *p_move) {
|
||||
return YES;
|
||||
}
|
BIN
src/utictactoe
Executable file
BIN
src/utictactoe
Executable file
Binary file not shown.
|
@ -1,11 +1,13 @@
|
|||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <getopt.h>
|
||||
#include <stdbool.h>
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
//#include <stdlib.h>
|
||||
//#include <stdio.h>
|
||||
//#include <getopt.h>
|
||||
//#include <stdbool.h>
|
||||
//#include <assert.h>
|
||||
//#include <string.h>
|
||||
//#include <unistd.h>
|
||||
#include "utictactoe.h"
|
||||
#include "common.h"
|
||||
#include "model.h"
|
||||
|
||||
#define OPTIONAL_ARGUMENT_IS_PRESENT \
|
||||
((optarg == NULL && optind < argc && argv[optind][0] != '-') \
|
||||
|
@ -26,6 +28,9 @@ void help(){
|
|||
}
|
||||
|
||||
int check_int_value ( char *value, int min, int max){
|
||||
struct a {
|
||||
int b;
|
||||
};
|
||||
char * end_ptr;
|
||||
int result;
|
||||
result = strtol(optarg, &end_ptr, 10);
|
||||
|
@ -135,6 +140,15 @@ int main(int argc, char* argv[]) {
|
|||
exit (EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
printf("Hello World!\n");
|
||||
s_move *test_move = create_empty_move();
|
||||
free_move(test_move);
|
||||
s_tictactoe *test_ttt = create_empty_tictactoe();
|
||||
free_tictactoe(test_ttt);
|
||||
s_utictactoe *test_uttt = create_empty_utictactoe(1);
|
||||
free_utictactoe(test_uttt);
|
||||
test_uttt = create_empty_utictactoe(2);
|
||||
free_utictactoe(test_uttt);
|
||||
test_uttt = create_empty_utictactoe(3);
|
||||
free_utictactoe(test_uttt);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue