Skip to content

Commit

Permalink
layout for v2 and fix v1
Browse files Browse the repository at this point in the history
  • Loading branch information
saolsen committed Feb 16, 2025
1 parent 1df1404 commit b4e61a8
Show file tree
Hide file tree
Showing 3 changed files with 291 additions and 5 deletions.
189 changes: 189 additions & 0 deletions tazar-v1/tazar.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,194 @@
#include <stdbool.h>
#include <stdint.h>

// Hex position in double coordinates.
// * Useful coordinates for game logic.
// * These are also axial coordinates if you ignore s.
// I chose to just always track s instead of calculating it.
typedef struct {
int q;
int r;
int s;
} CPos;

#define CPOS_RIGHT_UP (CPos){1, -1, 0}
#define CPOS_RIGHT (CPos){1, 0, -1}
#define CPOS_RIGHT_DOWN (CPos){0, 1, -1}
#define CPOS_LEFT_DOWN (CPos){-1, 1, 0}
#define CPOS_LEFT (CPos){-1, 0, 1}
#define CPOS_LEFT_UP (CPos){0, -1, 1}

bool cpos_eq(CPos a, CPos b);

CPos cpos_add(CPos a, CPos b);

// Hex position in double coordinates.
// * Useful coordinates for drawing.
typedef struct {
int x;
int y;
} DPos;

DPos dpos_from_cpos(CPos cpos);

CPos cpos_from_dpos(DPos dpos);

typedef enum {
PIECE_NONE = 0,
PIECE_EMPTY,
PIECE_CROWN,
PIECE_PIKE,
PIECE_HORSE,
PIECE_BOW,
} PieceKind;

typedef enum {
PLAYER_NONE = 0,
PLAYER_RED,
PLAYER_BLUE,
} Player;

typedef struct {
PieceKind kind;
Player player;
int id;
} Piece;

int piece_gold(PieceKind kind);

typedef enum {
ORDER_NONE = 0,
ORDER_MOVE,
ORDER_VOLLEY,
// muster
} OrderKind;

typedef struct {
OrderKind kind;
CPos target;
} Order;

typedef struct {
int piece_id;
Order orders[2];
int order_i;
} Activation;

typedef struct {
Player player;
Activation activations[2];
int activation_i;
} Turn;

typedef enum {
STATUS_NONE = 0,
STATUS_IN_PROGRESS,
STATUS_OVER,
} Status;

typedef struct {
Piece board[81];
Status status;
Player winner;
// hardcoded to 2 players
int gold[3]; // indexed by player, ignore gold[0].
Turn turn;
} Game;

bool game_eq(Game *a, Game *b);

Piece *board_at(Game *game, CPos pos);

void game_init_attrition_hex_field_small(Game *game);

// commands are you telling the game what to do. these are the things to generate I think.
typedef enum {
COMMAND_NONE = 0,
COMMAND_MOVE,
COMMAND_VOLLEY,
// muster
COMMAND_END_TURN,
} CommandKind;

// note: Probably want to specify the piece by CPos so that all possible commands
// can be packed into a single array. Will probably need that for RL.
typedef struct {
CommandKind kind;
int piece_id;
CPos target;
} Command;

bool command_eq(Command a, Command b);

int game_valid_commands(Command *buf, int max_commands, Game *game);


typedef enum {
VOLLEY_ROLL,
VOLLEY_HIT,
VOLLEY_MISS,
} VolleyResult;

void game_apply_command(Game *game, Player player, Command command, VolleyResult volley_result);


Command ai_select_command_heuristic(Game *game, Command *commands, int num_commands);


typedef struct {
double *scores;
int *passes;
int i;
} MCState;


MCState ai_mc_state_init(Game *game, Command *commands, int num_commands);

void ai_mc_state_cleanup(MCState *state);

void ai_mc_think(MCState *state, Game *game, Command *commands, int num_commands, int iterations);

Command ai_mc_select_command(MCState *state, Game *game, Command *commands, int num_commands);


typedef enum {
NODE_NONE,
NODE_DECISION,
NODE_CHANCE,
NODE_OVER,
} NodeKind;

typedef struct {
NodeKind kind;
Game game;
Command command;
uint32_t parent_i;
uint32_t first_child_i;
uint32_t num_children;
uint32_t num_children_to_expand;
uint32_t visits;
double total_reward;
double probability;
} Node;

typedef struct {
uint32_t root;
Node *nodes;
uintptr_t nodes_len;
uintptr_t nodes_cap;
} MCTSState;

MCTSState ai_mcts_state_init(Game *game, Command *commands, int num_commands);

void ai_mcts_state_cleanup(MCTSState *state);

void ai_mcts_think(MCTSState *state, Game *game, Command *commands, int num_commands, int iterations);

Command ai_mcts_select_command(MCTSState *state, Game *game, Command *commands, int num_commands);


int ai_test(void);

int ui_main(void);

#endif // TAZAR_H
104 changes: 99 additions & 5 deletions tazar/index.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,104 @@
<!DOCTYPE html>
<html lang="en">
<html>
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
html, body {
margin: 0;
padding: 0;
height: 100%;
font-family: system-ui;
background-color: #f0f0f0;
overflow: hidden;
}

#app-container {
display: flex;
flex-direction: column;
height: 100vh;
width: 100vw;
}

#turn-display {
text-align: center;
padding: 10px;
font-size: 24px;
font-weight: bold;
background-color: #e0e0e0;
}

#game-container {
display: flex;
flex-direction: column;
flex-grow: 1;
overflow: hidden;
position: relative;
}

#board-canvas {
flex-grow: 1;
max-width: 100%;
max-height: 100%;
object-fit: contain;
margin: 0 auto; /* Center the canvas */
}

#action-panels {
width: 100%;
background-color: #e0e0e0;
padding: 10px;
display: flex;
flex-direction: row;
justify-content: space-around;
align-items: center;
}

#action-panels > div {
flex: 1;
text-align: center;
padding: 5px;
}
</style>
</head>
<body>
hello tazar steve
<div id="app-container">
<div id="turn-display">Red Player's Turn</div>
<div id="game-container">
<canvas id="board-canvas"></canvas>
<div id="action-panels">
<div id="piece-actions">Piece Actions</div>
<div id="available-moves">Available Moves</div>
</div>
</div>
</div>
<script>
let canvas = document.getElementById('board-canvas');
let ctx = canvas.getContext('2d');

function resizeCanvas() {
// Get the game container's dimensions
let container = document.getElementById('game-container');
let actionPanels = document.getElementById('action-panels');

// Calculate available height
let availableHeight = container.clientHeight - actionPanels.clientHeight;

// Set canvas dimensions
canvas.width = container.clientWidth * window.devicePixelRatio;
canvas.height = availableHeight * window.devicePixelRatio;

// Scale the context
ctx.scale(window.devicePixelRatio, window.devicePixelRatio);

// Ensure canvas fits within container
canvas.style.width = container.clientWidth + 'px';
canvas.style.height = availableHeight + 'px';
}

// Initial resize
resizeCanvas();

// Resize on window change
window.addEventListener('resize', resizeCanvas);
</script>
</body>
</html>
</html>
3 changes: 3 additions & 0 deletions tazar/tazar.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@
#include <stdbool.h>
#include <stdint.h>

typedef struct {

} Game;

#endif // TAZAR_H

0 comments on commit b4e61a8

Please sign in to comment.