src/game.c

Wed, 19 Mar 2014 15:36:54 +0100

author
Mike Becker <universe@uap-core.de>
date
Wed, 19 Mar 2014 15:36:54 +0100
changeset 7
41468077b5bb
parent 6
daaf6e5b3501
child 8
52d742aee695
permissions
-rw-r--r--

implemented chess board and remis/surrender messages

universe@6 1 /*
universe@6 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
universe@6 3 *
universe@6 4 * Copyright 2014 Mike Becker. All rights reserved.
universe@6 5 *
universe@6 6 * Redistribution and use in source and binary forms, with or without
universe@6 7 * modification, are permitted provided that the following conditions are met:
universe@6 8 *
universe@6 9 * 1. Redistributions of source code must retain the above copyright
universe@6 10 * notice, this list of conditions and the following disclaimer.
universe@6 11 *
universe@6 12 * 2. Redistributions in binary form must reproduce the above copyright
universe@6 13 * notice, this list of conditions and the following disclaimer in the
universe@6 14 * documentation and/or other materials provided with the distribution.
universe@6 15 *
universe@6 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
universe@6 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
universe@6 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
universe@6 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
universe@6 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
universe@6 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
universe@6 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
universe@6 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
universe@6 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
universe@6 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
universe@6 26 * POSSIBILITY OF SUCH DAMAGE.
universe@6 27 *
universe@6 28 */
universe@6 29
universe@6 30 #include "game.h"
universe@7 31 #include "input.h"
universe@7 32 #include <ncurses.h>
universe@7 33 #include <string.h>
universe@7 34
universe@7 35 static const uint8_t boardx = 10, boardy = 10;
universe@7 36
universe@7 37 static void draw_board(Board board) {
universe@7 38
universe@7 39 for (uint8_t y = 0 ; y < 8 ; y++) {
universe@7 40 for (uint8_t x = 0 ; x < 8 ; x++) {
universe@7 41 uint8_t col = board[y][x] & COLOR_MASK;
universe@7 42 uint8_t piece = board[y][x] & PIECE_MASK;
universe@7 43 char piecec = ' ';
universe@7 44 switch (piece) {
universe@7 45 case PAWN: piecec = 'P'; break;
universe@7 46 case ROOK: piecec = 'R'; break;
universe@7 47 case KNIGHT: piecec = 'N'; break;
universe@7 48 case BISHOP: piecec = 'B'; break;
universe@7 49 case QUEEN: piecec = 'Q'; break;
universe@7 50 case KING: piecec = 'K'; break;
universe@7 51 }
universe@7 52
universe@7 53 attrset((col == WHITE ? A_BOLD : A_DIM) |
universe@7 54 COLOR_PAIR((y&1)==(x&1) ? COL_WB : COL_BW));
universe@7 55
universe@7 56 mvaddch(boardy-y, boardx+x*3, ' ');
universe@7 57 mvaddch(boardy-y, boardx+x*3+1, piecec);
universe@7 58 mvaddch(boardy-y, boardx+x*3+2, ' ');
universe@7 59 }
universe@7 60 }
universe@7 61
universe@7 62 attrset(A_NORMAL);
universe@7 63 for (uint8_t i = 0 ; i < 8 ; i++) {
universe@7 64 mvaddch(boardy+1, boardx+i*3+1, 'a'+i);
universe@7 65 mvaddch(boardy-i, boardx-2, '1'+i);
universe@7 66 }
universe@7 67 }
universe@7 68
universe@7 69 static int sendmove(int opponent) {
universe@7 70 const size_t buflen = 8;
universe@7 71 char move[buflen];
universe@7 72 _Bool remisrejected = FALSE;
universe@7 73
universe@7 74 while (1) {
universe@7 75 move(boardy+3, 0);
universe@7 76 if (remisrejected) {
universe@7 77 printw(
universe@7 78 "Use chess notation to enter your move.\n"
universe@7 79 "Remis offer rejected - type 'surr' to surrender. \n\n"
universe@7 80 "Type your move: ");
universe@7 81 } else {
universe@7 82 printw(
universe@7 83 "Use chess notation to enter your move.\n"
universe@7 84 "Or type 'surr' to surrender or 'remis' to offer remis.\n\n"
universe@7 85 "Type your move: ");
universe@7 86 }
universe@7 87 clrtoeol();
universe@7 88 refresh();
universe@7 89 getnstr(move, buflen);
universe@7 90
universe@7 91 if (strncmp(move, "surr", buflen) == 0) {
universe@7 92 printw("You surrendered!");
universe@7 93 net_send_code(opponent, NETCODE_SURRENDER);
universe@7 94 return 1;
universe@7 95 } else if (strncmp(move, "remis", buflen) == 0) {
universe@7 96 if (!remisrejected) {
universe@7 97 net_send_code(opponent, NETCODE_REMIS);
universe@7 98 printw("Remis offer sent - waiting for acceptance...");
universe@7 99 refresh();
universe@7 100 if (net_recieve_code(opponent) == NETCODE_ACCEPT) {
universe@7 101 printw("\rRemis accepted!");
universe@7 102 clrtoeol();
universe@7 103 return 1;
universe@7 104 } else {
universe@7 105 remisrejected = TRUE;
universe@7 106 }
universe@7 107 }
universe@7 108 } else {
universe@7 109 // TODO: validate move syntactically
universe@7 110 // TODO: send move and await acceptance
universe@7 111 }
universe@7 112 }
universe@7 113 }
universe@7 114
universe@7 115 static int recvmove(int opponent) {
universe@7 116
universe@7 117 while (1) {
universe@7 118 move(boardy+3, 0);
universe@7 119 printw("Awaiting opponent move...");
universe@7 120 clrtoeol();
universe@7 121 refresh();
universe@7 122
universe@7 123 // TODO: nonblocking
universe@7 124 uint32_t code = net_recieve_code(opponent);
universe@7 125 switch (code) {
universe@7 126 case NETCODE_SURRENDER:
universe@7 127 printw("\rYour opponent surrendered!");
universe@7 128 clrtoeol();
universe@7 129 return 1;
universe@7 130 case NETCODE_REMIS:
universe@7 131 if (prompt_yesno(
universe@7 132 "\rYour opponent offers remis - do you accept")) {
universe@7 133 printw("\rRemis accepted!");
universe@7 134 clrtoeol();
universe@7 135 net_send_code(opponent, NETCODE_ACCEPT);
universe@7 136 return 1;
universe@7 137 } else {
universe@7 138 net_send_code(opponent, NETCODE_DECLINE);
universe@7 139 }
universe@7 140 break;
universe@7 141 case NETCODE_MOVE:
universe@7 142 // TODO: receive move
universe@7 143 // TODO: validate move and accept/reject
universe@7 144 return 0;
universe@7 145 }
universe@7 146 }
universe@7 147 }
universe@6 148
universe@6 149 void game_start(Settings *settings, int opponent) {
universe@7 150 _Bool myturn = is_server(settings) ==
universe@7 151 (settings->gameinfo.servercolor == WHITE);
universe@7 152 _Bool running;
universe@6 153
universe@7 154 Board board = {
universe@7 155 {WROOK, WKNIGHT, WBISHOP, WQUEEN, WKING, WBISHOP, WKNIGHT, WROOK},
universe@7 156 {WPAWN, WPAWN, WPAWN, WPAWN, WPAWN, WPAWN, WPAWN, WPAWN},
universe@7 157 {0, 0, 0, 0, 0, 0, 0, 0},
universe@7 158 {0, 0, 0, 0, 0, 0, 0, 0},
universe@7 159 {0, 0, 0, 0, 0, 0, 0, 0},
universe@7 160 {0, 0, 0, 0, 0, 0, 0, 0},
universe@7 161 {BPAWN, BPAWN, BPAWN, BPAWN, BPAWN, BPAWN, BPAWN, BPAWN},
universe@7 162 {BROOK, BKNIGHT, BBISHOP, BQUEEN, BKING, BBISHOP, BKNIGHT, BROOK}
universe@7 163 };
universe@7 164
universe@7 165 do {
universe@7 166 clear();
universe@7 167 draw_board(board);
universe@7 168 if (myturn) {
universe@7 169 running = !sendmove(opponent);
universe@7 170 } else {
universe@7 171 running = !recvmove(opponent);
universe@7 172 flushinp(); // flush any input the user hacked in while waiting
universe@7 173 }
universe@7 174 myturn ^= 1;
universe@7 175 } while (running);
universe@7 176
universe@7 177 mvaddstr(getmaxy(tchess_window)-1, 0,
universe@7 178 "Game has ended. Press any key to leave...");
universe@7 179 getch();
universe@6 180 }

mercurial