ESP Chess code ready for Apple II software dev.

This commit is contained in:
equant 2021-10-03 09:30:17 -07:00
parent b16e31c9a3
commit b7da06f6f9
7 changed files with 44 additions and 142 deletions

View File

@ -105,10 +105,10 @@ unsigned int Apple2Idiot::write_string_to_shared_ram(String string_to_send, unsi
write_data(ESP_COMMAND_ADDRESS, 12);
unsigned int c = 2;
for (c=0; c < string_to_send.length(); c++) {
Serial.print("A(");
Serial.print(c);
Serial.print("): ");
Serial.println(string_to_send[c]);
//Serial.print("A(");
//Serial.print(c);
//Serial.print("): ");
//Serial.println(string_to_send[c]);
write_data(address+c, string_to_send[c]);
}
write_data(address+c, EOS);

View File

@ -13,4 +13,7 @@ Global variables use 39456 bytes (12%) of dynamic memory, leaving 288224 bytes f
Sketch uses 887406 bytes (67%) of program storage space. Maximum is 1310720 bytes.
Global variables use 39456 bytes (12%) of dynamic memory, leaving 288224 bytes for local variables. Maximum is 327680 bytes.
Sketch uses 887698 bytes (67%) of program storage space. Maximum is 1310720 bytes.
Global variables use 39648 bytes (12%) of dynamic memory, leaving 288032 bytes for local variables. Maximum is 327680 bytes.
```

View File

@ -1,86 +0,0 @@
10 CA = 49664
20 AA = CA +1
30 CC$ = ""
35 CITY$ = ""
100 PRINT
110 PRINT "----"
120 PRINT "MENU"
130 PRINT "(1) COUNTRY","",CC$
140 PRINT "(2) CITY","",CITY$
150 PRINT "(3) FETCH WEATHER"
160 PRINT "(4) SHOW WEATHER"
170 PRINT "(0) QUIT"
175 PRINT
180 INPUT "SELECT: ";S
190 IF S >4 THEN GOTO 100
200 IF S = 0 THEN END
210 IF S = 1 THEN GOTO 1000
220 IF S = 2 THEN GOTO 2000
230 IF S = 3 THEN GOTO 3000
240 IF S = 4 THEN GOTO 4000
250 GOTO 100
1000 INPUT "COUNTRY CODE: ";CC$
1010 WS$ = CC$
1020 WA = AA +1
1030 GOSUB 10000
1040 POKE AA,200
1050 GOTO 100
2000 INPUT "CITY: ";CITY$
2010 WS$ = CITY$
2020 WA = AA +1
2030 GOSUB 10000
2040 POKE AA,201
2050 GOTO 100
3000 POKE AA,205
3010 GOTO 100
4000 HOME
4005 MAXSTR = 0
4010 RA = AA +1
4015 GOSUB 11000
4020 TEMP$ = RS$
4025 RA = RA + LEN(RS$) +1
4030 GOSUB 11000
4035 FOOM$ = RS$
4040 RA = RA + LEN(RS$) +1
4045 GOSUB 11000
4050 WSPEED$ = RS$
4055 RA = RA + LEN(RS$) +1
4060 GOSUB 11000
4065 WDIR$ = RS$
4070 RA = RA + LEN(RS$) +1
4075 GOSUB 11000
4080 WD1$ = RS$
4085 RA = RA + LEN(RS$) +1
4090 GOSUB 11000
4095 WD2$ = RS$
4150 PRINT
4155 PRINT CITY$ +"/" +CC$ +" " +WD1$
4160 PRINT "-----------------------------"
4165 PRINT "TEMP: " +TEMP$ +"K"
4170 PRINT "HUMIDITY: " +FOOM$ +"%"
4175 PRINT "WIND SPEED: " +WSPEED$ +" m/s"
4177 REM PRINT
4180 REM PRINT WD1$ + ", " + WD2$
4500 GOTO 100
10000 REM ============ WRITE WS$ TO WA IN RAM ================
10010 FOR N = 0 TO LEN(WS$) -1
10020 P = WA +N
10030 C$ = MID$ (WS$,N +1,1)
10040 REM PRINT C$, ASC (C$)
10050 POKE P, ASC(C$)
10060 NEXT N
10070 POKE P +1,0
10080 RETURN
11000 REM ============ READ STRING FROM RAM =================
11001 REM RA ADDRESS TO START READ
11002 REM MAXSTR IS LIMIT TO STR SIZE WE READ
11020 IF MAXSTR = 0 THEN MAXSTR = 250
11040 RS$ = ""
11060 FOR N = 0 TO MAXSTR
11080 P = RA +N
11100 C = PEEK(P)
11120 IF C = 0 THEN GOTO 11200
11140 RS$ = RS$ + CHR$(C)
11160 REM PRINT N+1,P,C, CHR$ (C)
11180 NEXT N
11200 RETURN

View File

@ -4,7 +4,7 @@
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include <Apple2Idiot.h>
//#include "chess_commands.h"
#include "chess_commands.h"
#include "a2i_chess.h"
@ -23,14 +23,11 @@ byte Chess::handleCommand(byte command) {
move_string = a2i->read_string_from_ram(SHARED_RAM_START_ADDRESS);
Serial.println("Received move: ["+move_string+"]");
byte result = makeMove(move_string);
return result;
/*
switch(result) {
case CHESS_INVALID_MOVE: {
break;
}
int address_counter = a2i->write_data(result, SHARED_RAM_START_ADDRESS);
getBoard();
for (int i=0; i<9; i++) {
address_counter = a2i->write_string_to_shared_ram(game_board[i], address_counter + 1);
}
*/
break;
}
//case COMMAND_SET_CITY:
@ -257,16 +254,23 @@ void Chess::getBoard() {
char* pch = NULL;
pch = strtok((char *)doc["board"].as<char *>(), "\n");
//Serial.print(" pch:");Serial.println(pch);
int row_count = 0;
while (pch != NULL) {
char my_line[30];
strcpy(my_line, pch);
removeSubstr(my_line, "[37m");
removeSubstr(my_line, "[0m");
removeSubstr(my_line, "\n");
Serial.print(" | "); Serial.print(my_line); Serial.println(" |");
char board_line[30];
strcpy(board_line, pch);
removeSubstr(board_line, "[37m");
removeSubstr(board_line, "[0m");
removeSubstr(board_line, "\n");
Serial.print("(");Serial.print(row_count);Serial.print(")");
Serial.print(board_line); Serial.println("|");
strcpy(game_board[row_count], board_line); // valid
pch = strtok(NULL, "\n");
row_count++;
}
Serial.println();
for (int i=0; i<9; i++) {
Serial.print("[");Serial.print(game_board[i]);Serial.println("]");
}
//return (char *)doc["bestNext"].as<char *>();
}
} else {
Serial.println("Error on HTTP request");

View File

@ -5,40 +5,8 @@
#include <HTTPClient.h>
#include <A2I_commands.h>
#include <Apple2Idiot.h>
//#include "chess_commands.h"
#include "chess_commands.h"
/* Apple II <-> ESP Commands */
#define CHESS_GET_AI_MOVE 20
#define CHESS_GET_GAME_STATUS 22
#define CHESS_GET_BOARD 23
#define CHESS_MAKE_MOVE 21
/* Responses */
#define CHESS_INVALID_MOVE 123
#define CHESS_VALID_MOVE 124
/*
* Responses from API...
* "in_progress"
* "black_won"
* "white_won"
* "white_won_resign"
* "black_won_resign"
* "stalemate"
* "insufficient_material"
* "fifty_rule_move"
* "threefold_repitition"
* "unknown"
*/
#define STATUS_IN_PROGRESS 200
#define STATUS_BLACK_WON 201
#define STATUS_WHITE_WON 202
#define STATUS_STALEMATE 205
#define STATUS_FIFTY_RULE_MOVE 207
#define STATUS_UNKNOWN 209
#define STATUS_UNHANDLED 210
#define STATUS_ERROR 211
#define MAX_GAME_SIZE 110 * 4 // This is probably not enough, but it's fine for development.
// https://chess.stackexchange.com/questions/2506/what-is-the-average-length-of-a-game-of-chess
@ -57,6 +25,19 @@ class Chess {
char game_status[25];
//char game_board[9][20];
char game_board[9][22] = {
"8 r n b q k b n r",
"7 p p p p p p p p",
"6 . . . . . . . .",
"5 . . . . . . . .",
"4 . . . . . . . .",
"3 . . . . . . . .",
"2 P P P P P P P P",
"1 R N B Q K B N R",
" a b c d e f g h"
};
void init(Apple2Idiot *a2ip, HTTPClient *httpp);
char* getGameStatus(char* game_status);
byte makeMove(String move_string);

View File

@ -69,8 +69,8 @@ void setup() {
current_app_id = app_ids[0];
//chess_app.validateMove("g8f6");
chess_app.makeMove("g8f6");
chess_app.getBoard();
//chess_app.makeMove("g8f6");
//chess_app.getBoard();
}
/*################################################

View File

@ -1,5 +1,5 @@
#ifndef A2I_CHESS_H
#define A2I_CHESS_H
#ifndef A2I_CHESS_COMMANDS_H
#define A2I_CHESS_COMMANDS_H
/* Apple II <-> ESP Commands */
#define CHESS_GET_AI_MOVE 20