summaryrefslogtreecommitdiff
path: root/apps/plugins/othelo.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/plugins/othelo.c')
-rw-r--r--apps/plugins/othelo.c348
1 files changed, 173 insertions, 175 deletions
diff --git a/apps/plugins/othelo.c b/apps/plugins/othelo.c
index 8298b1d0c7..0ac7828367 100644
--- a/apps/plugins/othelo.c
+++ b/apps/plugins/othelo.c
@@ -1,183 +1,47 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2003 Blue Chip
11 *
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
14 *
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
17 *
18 ****************************************************************************/
19/* 1/*
20 Designed, Written, AI Bots, the lot ...BlueChip =8ĒD# 2 Designed, Written, AI Bots, the lot ...BlueChip =8ĒD#
21 3
22 Thanks espcially to 4 Thanks espcially to
23 Hardeep, DevZer0, LinusN 5 DevZer0, LinusN, Zagor
24 for their help with understanding Rockbox & the SDK 6 for their help with understanding Rockbox & the SDK
25*/
26
27#include "plugin.h"
28#ifdef HAVE_LCD_BITMAP
29
30/* the following #define had to be taken from Button.c
31 'cos it is not defined in the header!! */
32/* how long until repeat kicks in */
33#define REPEAT_START 6
34
35/* player types */
36#define HUMAN false
37#define AIBOT true
38
39/* for domove() */
40#define CHECK false
41#define MAKE true
42 7
43/* screen coords - top left x&y */ 8 Please note that the code formatting is not that which was
44 /* game over */ 9 produced originally, but has been updated by whoever
45#define go_tlx 71 10 ported it to the plugin system.
46#define go_tly 17 11 I am sure it was done with good reason, so I have not
47 /* WiNS */ 12 redone it!
48#define win_tlx 63 13*/
49#define win_tly 1
50 /* DRaW */
51#define draw_tlx 59
52#define draw_tly 1
53 /* scores */
54#define sc_tlx 65
55#define sc_tly 39
56 /* logo */
57#define logo_tlx 65
58#define logo_tly 2
59
60/* board sqaures -
61 * there are a number of routines that expect these values asis
62 * do not try to play with these, you will likely kill the program
63 */
64#define PLAYERX 0
65#define PLAYERO 1
66#define POSS 2
67#define CHOICE 3
68#define EMPTY 4
69#define BORDER 5
70
71/* Who gets first turn */
72#define FIRST PLAYERX
73#define DF_PLX HUMAN
74#define DF_AIX NONE
75#define DF_PLO AIBOT
76#define DF_AIO WEAK
77
78/* Oponent skill level / help level
79 * -------- ---------------------------------------------------
80 * NONE no ai / no help
81 * WEAK random valid move / show all possible
82 * AVERAGE most pieces (random) / all + most pieces
83 * SMART most pieces (weighted/random) / all + weighted
84 * EXPERT
85 * GURU
86 */
87#define NONE 0
88#define WEAK 1
89#define AVERAGE 2
90#define SMART 3
91#define EXPERT 4
92#define GURU 5
93#define BEST 3 /* the best ai alogrithm currently available */
94
95/* these are for code clarity, do not change them! */
96#define LEFT 0x08
97#define RIGHT 0x04
98#define UP 0x02
99#define DOWN 0x01
100 14
101/* This represents the maximum number of possible moves 15/*
102 * I have no idea what the real maximum is, buts tests 16 * Version Date Who Comment
103 * suggest about 10 17 * -------- -------- ---- ------------------------------------------------
18 * 1.3 20030729 BC Fixed display bug introduced by port to plugin
19 * Updated documentation
20 * 1.2 2003 Ported to new plugin system
21 * 1.1 20030625 BC Flash board when invalid move to used aquare
22 * Fixed "pause computer" for real harware!
23 * Added USB_CONNECTED support
24 * Ensure correct fonts on the way in and out
25 * 1.0 20030622 BC Release
26 *
27 *
28 * Todo:
29 * # More AI :)
30 * # Reintroduce suspend feature under plugin system
104 */ 31 */
105#define MAXPOSS 20
106
107struct move
108{
109 int x;
110 int y;
111 int taken;
112 int rank;
113 bool player;
114};
115
116
117/*===================================================================
118 * Procedure prototypes
119 *==================================================================*/
120static void changeplayer(bool pl);
121
122static int othstrlen(char* s);
123static void othprint(unsigned char x, unsigned char y, char ch, bool upd);
124static void othprints(unsigned char x, unsigned char y, char* s, bool upd);
125
126static void initscreen(void);
127static void show_board(void);
128static void flashboard(void);
129static void show_grid(void);
130static void show_score(bool turn);
131static void show_players(void);
132static void show_f3(bool playing);
133static void hilite(struct move* move, bool on);
134static void show_endgame(unsigned char scx, unsigned char sco);
135
136static void initboard(void);
137static int getmove(struct move* move, struct move* plist,
138 unsigned char* pcnt, bool turn);
139static int checkmove(unsigned char x, unsigned char y, bool pl,
140 unsigned char dir, bool type);
141static void domove(struct move* move, bool type);
142
143static bool calcposs(struct move* plist, unsigned char* pcnt, bool turn);
144static int getplist(struct move* plist, unsigned char pl);
145static unsigned char reduceplist(struct move* plist, unsigned char pcnt,
146 unsigned char ai_help);
147static void smartranking(struct move* plist, unsigned char pcnt);
148static int plist_bytaken(const void* m1, const void* m2);
149static int plist_byrank(const void* m1, const void* m2);
150static void clearposs(void);
151 32
152 33
153/*=================================================================== 34#ifdef HAVE_LCD_BITMAP
154 * *static* local global variables
155 *==================================================================*/
156 35
36/* Plugin header */
37#include "plugin.h"
157static struct plugin_api* rb; 38static struct plugin_api* rb;
158 39
159/* score */ 40/***************************************************************************/
160static struct 41/***************************************************************************/
161 { 42/* OTHFONT.H */
162 int x; 43/***************************************************************************/
163 int o; 44/***************************************************************************/
164 } score;
165
166/* 8x8 with borders */
167static unsigned char board[10][10];
168
169/* player=HUMAN|AIBOT */
170static bool player[2] = {DF_PLX, DF_PLO};
171
172/* AI = WEAK|AVERAGE|SMART|EXPERT|GURU
173 Help=NONE|WEAK|AVERAGE|SMART|EXPERT|GURU */
174static unsigned char ai_help[2] = {DF_AIX, DF_AIO};
175
176/* is a game under way */
177static bool playing = false;
178
179/* who's turn is it? */
180static bool turn = FIRST;
181 45
182/* Don't reorder this array - you have been warned! */ 46/* Don't reorder this array - you have been warned! */
183enum othfontc { 47enum othfontc {
@@ -443,6 +307,13 @@ static unsigned char othfont[of_eos][6] = {
443 307
444}; 308};
445 309
310
311/***************************************************************************/
312/***************************************************************************/
313/* OTHLOGO.H */
314/***************************************************************************/
315/***************************************************************************/
316
446/* 317/*
447 318
448######### # # ### ## ## ### 319######### # # ### ## ## ###
@@ -575,12 +446,135 @@ static void showlogo(int x, int y, bool on)
575} 446}
576 447
577 448
449/***************************************************************************/
450/***************************************************************************/
451/* OTHELO.H */
452/***************************************************************************/
453/***************************************************************************/
454
455
456/* the following #define had to be taken from Button.c
457 'cos it is not defined in the header!! */
458/* how long until repeat kicks in */
459#define REPEAT_START 6
460
461/* player types */
462#define HUMAN false
463#define AIBOT true
464
465/* for domove() */
466#define CHECK false
467#define MAKE true
468
469/* screen coords - top left x&y */
470 /* game over */
471#define go_tlx 71
472#define go_tly 17
473 /* WiNS */
474#define win_tlx 63
475#define win_tly 1
476 /* DRaW */
477#define draw_tlx 59
478#define draw_tly 1
479 /* scores */
480#define sc_tlx 65
481#define sc_tly 39
482 /* logo */
483#define logo_tlx 65
484#define logo_tly 2
485
486/* board sqaures -
487 * there are a number of routines that expect these values asis
488 * do not try to play with these, you will likely kill the program
489 */
490#define PLAYERX 0
491#define PLAYERO 1
492#define POSS 2
493#define CHOICE 3
494#define EMPTY 4
495#define BORDER 5
496
497/* Who gets first turn */
498#define FIRST PLAYERX
499#define DF_PLX HUMAN
500#define DF_AIX NONE
501#define DF_PLO AIBOT
502#define DF_AIO WEAK
503
504/* Oponent skill level / help level
505 * -------- ---------------------------------------------------
506 * NONE no ai / no help
507 * WEAK random valid move / show all possible
508 * AVERAGE most pieces (random) / all + most pieces
509 * SMART most pieces (weighted/random) / all + weighted
510 * EXPERT
511 * GURU
512 */
513#define NONE 0
514#define WEAK 1
515#define AVERAGE 2
516#define SMART 3
517#define EXPERT 4
518#define GURU 5
519#define BEST 3 /* the best ai alogrithm currently available */
520
521/* these are for code clarity, do not change them! */
522#define LEFT 0x08
523#define RIGHT 0x04
524#define UP 0x02
525#define DOWN 0x01
526
527/* This represents the maximum number of possible moves
528 * I have no idea what the real maximum is, buts tests
529 * suggest about 10
530 */
531#define MAXPOSS 20
532
533struct move
534{
535 int x;
536 int y;
537 int taken;
538 int rank;
539 bool player;
540};
541
542
543/*===================================================================
544 * local global variables
545 * THIS IS THE DATA THAT NEEDS TO BE SAVED TO ALLOW THE GAME
546 * TO CONTINUE ...THE CONTINUE FEATURE DOES NOT WORK UNDER THE
547 * NEW PLUGIN SYSTEM!
548 *==================================================================*/
549
550/* score */
551static struct
552 {
553 int x;
554 int o;
555 } score;
556
557/* 8x8 with borders */
558static unsigned char board[10][10];
559
560/* player=HUMAN|AIBOT */
561static bool player[2] = {DF_PLX, DF_PLO};
562
563/* AI = WEAK|AVERAGE|SMART|EXPERT|GURU
564 Help=NONE|WEAK|AVERAGE|SMART|EXPERT|GURU */
565static unsigned char ai_help[2] = {DF_AIX, DF_AIO};
566
567/* is a game under way */
568static bool playing = false;
569
570/* who's turn is it? */
571static bool turn = FIRST;
572
578/******************************************************************** 573/********************************************************************
579 * strlen ofr use with othello print system 574 * strlen ofr use with othello print system
580 ********************************************************************/ 575 ********************************************************************/
581static int othstrlen(char* s) 576static int othstrlen(char* s)
582{ 577{
583
584 int i; 578 int i;
585 579
586 for(i=0; s[i]!=of_eos; i++); 580 for(i=0; s[i]!=of_eos; i++);
@@ -730,10 +724,14 @@ static void show_players(void)
730 724
731 if (player[PLAYERX]==AIBOT) 725 if (player[PLAYERX]==AIBOT)
732 scs[2] = of_c; 726 scs[2] = of_c;
727 else
728 scs[2] = of_h;
733 scs[4] = ai_help[PLAYERX] +of_0; 729 scs[4] = ai_help[PLAYERX] +of_0;
734 730
735 if (player[PLAYERO]==AIBOT) 731 if (player[PLAYERO]==AIBOT)
736 scs[8] = of_c; 732 scs[8] = of_c;
733 else
734 scs[8] = of_h;
737 scs[10] = ai_help[PLAYERO] +of_0; 735 scs[10] = ai_help[PLAYERO] +of_0;
738 736
739 othprints( 2,58, &scs[0], true); 737 othprints( 2,58, &scs[0], true);
@@ -1151,11 +1149,11 @@ static unsigned char reduceplist(struct move* plist, unsigned char pcnt, unsigne
1151 /* ------------------------------------------------- */ 1149 /* ------------------------------------------------- */
1152 case GURU: 1150 case GURU:
1153 break; 1151 break;
1154 /* ------------------------------------------------- */ 1152 /* ------------------------------------------------- */
1155 case EXPERT: 1153 case EXPERT:
1156 break; 1154 break;
1157 /* ------------------------------------------------- */ 1155 /* ------------------------------------------------- */
1158 /* this player will favour certain known moves */ 1156 /* this player will favour certain known moves */
1159 case SMART: 1157 case SMART:
1160 if (pcnt>1) 1158 if (pcnt>1)
1161 { 1159 {
@@ -1167,8 +1165,8 @@ static unsigned char reduceplist(struct move* plist, unsigned char pcnt, unsigne
1167 pcnt = i; 1165 pcnt = i;
1168 } 1166 }
1169 /* FALL THROUGH */ 1167 /* FALL THROUGH */
1170 /* ------------------------------------------------- */ 1168 /* ------------------------------------------------- */
1171 /* reduce possibilites to "most pieces taken" */ 1169 /* reduce possibilites to "most pieces taken" */
1172 case AVERAGE: 1170 case AVERAGE:
1173 if (pcnt>1) 1171 if (pcnt>1)
1174 { 1172 {
@@ -1179,7 +1177,7 @@ static unsigned char reduceplist(struct move* plist, unsigned char pcnt, unsigne
1179 pcnt = i; 1177 pcnt = i;
1180 } 1178 }
1181 break; 1179 break;
1182 /* ------------------------------------------------- */ 1180 /* ------------------------------------------------- */
1183 default: 1181 default:
1184 // you should never get here! 1182 // you should never get here!
1185 break; 1183 break;
@@ -1353,7 +1351,7 @@ enum plugin_status plugin_start(struct plugin_api* api, void* parameter)
1353 TEST_PLUGIN_API(api); 1351 TEST_PLUGIN_API(api);
1354 (void)parameter; 1352 (void)parameter;
1355 rb = api; 1353 rb = api;
1356 1354
1357 quit = false; 1355 quit = false;
1358 1356
1359 do /* while !quit */ 1357 do /* while !quit */