示例#1
0
void rungame(void)
{
    int ret, role = initrole, race = initrace, gend = initgend, align = initalign;
    int fd = -1;
    char plname[BUFSZ];
    fnchar filename[1024];
    fnchar savedir[BUFSZ];
    long t;

    if (!get_gamedir(SAVE_DIR, savedir)) {
        curses_raw_print("Could not find where to put the logfile for a new game.");
        return;
    }

    if (!player_selection(&role, &race, &gend, &align, random_player))
        return;

    strncpy(plname, settings.plname, PL_NSIZ);
    /* The player name is set to "wizard" (again) in nh_start_game, so setting
     * it here just prevents wizmode player from being asked for a name. */
    if (ui_flags.playmode == MODE_WIZARD)
        strcpy(plname, "wizard");

    while (!plname[0])
        curses_getline("what is your name?", plname);
    if (plname[0] == '\033') /* canceled */
        return;

    t = (long)time(NULL);
#if defined(WIN32)
    snwprintf(filename, 1024, L"%ls%ld_%hs.nhgame", savedir, t, plname);
#else
    snprintf(filename, 1024, "%s%ld_%s.nhgame", savedir, t, plname);
#endif
    fd = sys_open(filename, O_TRUNC | O_CREAT | O_RDWR, FILE_OPEN_MASK);
    if (fd == -1) {
        curses_raw_print("Could not create the logfile.");
        return;
    }

    create_game_windows();
    if (!nh_start_game(fd, plname, role, race, gend, align, ui_flags.playmode)) {
        destroy_game_windows();
        close(fd);
        return;
    }

    load_keymap(); /* need to load the keymap after the game has been started */
    ret = commandloop();
    free_keymap();
    close(fd);

    destroy_game_windows();
    cleanup_messages();
    game_ended(ret, filename);
}
示例#2
0
void CheckerBoardWidget::move()
{
    movement m;
    m.startPoint.line = firstClick.y();
    m.startPoint.column = firstClick.x();
    m.endPoint.line = secondClick.y();
    m.endPoint.column = secondClick.x();

    qDebug() << "[Move] ";
    qDebug() << "[MOVEMENT] : start (" << m.startPoint.line << ", " << m.startPoint.column << ")";
    qDebug() << "[MOVEMENT] : end (" << m.endPoint.line << ", " << m.endPoint.column << ")";


    if(test_and_execute_movement(player_color, m, &board) != 0) {
        //Valid movement, do it for gui
        qDebug() << "movement is ok";

        promote_checkers(&board, player_color);   //Check if daughter is made

        clearLists();   // Clear all the gui gameboard

        placeCheckers(); // Reconstruct the new GUI gameboard

        //Check if there is a winner
        if(game_ended(&board) == BLACK_CHECKER) {
            _player.color == BLACK_CHECKER ? emit sendWinner(_player) : emit sendWinner(_opponent_player);
        }
        if(game_ended(&board) == WHITE_CHECKER) {
            _player.color == WHITE_CHECKER ? emit sendWinner(_opponent_player) : emit sendWinner(_player);
        }


        preventAction(player_color);
        changePlayer();

//        qDebug() << "ppppppppppppppppppppppppppppppp" << (char *) board;
        emit sendCheckerboard(board);
    }
}
示例#3
0
void
net_loadgame(void)
{
    char buf[BUFSZ];
    struct nhnet_game *gamelist;
    struct nh_menulist menu;
    int id, size, i, ret, pick[1];

    gamelist = nhnet_list_games(FALSE, FALSE, &size);
    if (!gamelist) {
        curses_msgwin("Failed to retrieve the list of saved games.",
                      krc_notification);
        return;
    }
    if (!size) {
        curses_msgwin("No saved games found.", krc_notification);
        return;
    }

    init_menulist(&menu);

    for (i = 0; i < size; i++) {
        if (gamelist[i].status == LS_DONE || gamelist[i].status == LS_INVALID)
            continue;
        describe_game(buf, gamelist[i].status, &gamelist[i].i);
        id = (gamelist[i].status == LS_IN_PROGRESS) ? 0 : gamelist[i].gameid;
        add_menu_item(&menu, id, buf, 0, FALSE);
    }

    curses_display_menu(&menu, "saved games", PICK_ONE, PLHINT_ANYWHERE, pick,
                        curses_menu_callback);

    if (*pick == CURSES_MENU_CANCELLED)
        return;

    id = pick[0];

    create_game_windows();

    ret = playgame(id, FM_PLAY);

    destroy_game_windows();
    cleanup_messages();

    game_ended(ret, NULL, TRUE);
}
示例#4
0
void QGameWindow::start_game() {
    assert(not player_a);
    assert(not player_b);
    assert(not thread);

	delete board;
	board=new BoardC4();
	board_widget->board=board;

    player_a=qplayer_a.get_player();
    player_b=qplayer_b.get_player();

    thread=new QGameThread(&mutex,player_a,player_b,board);

    connect(thread,SIGNAL(move_played()),board_widget,SLOT(update()));
    connect(thread,SIGNAL(finished()),this,SLOT(game_ended()));

    thread->start();

    start.setEnabled(false);
}
示例#5
0
int main(void)
{
     char player1 = CROSS;
     char player2 = NOUGHT;

     char winner;
     char currnt_player = player1;
     char board[NUM_ALL];
     char *br;
     bool ended = false;
     int nrounds = 0;
     int mode;

     int wplayer1 = 0;
     int wplayer2 = 0;
     int nreplays = 0;
     int nstalemates = 0;


     mode = select_mode();
     if (mode == 1) {
        max_depth = set_diff();
     }
     while (!ended) {

        if (nrounds == 0) {
           for (int i = 0; i < NUM_ALL; ++i) {
              board[i] = ' ';
            }
            br = &board[0];
            simulate(br);
        }
        if (mode == 1) {
           if (currnt_player == player1) {
              human_turn(br, currnt_player, &nrounds);
           } else {
              computer_turn(br, player1, currnt_player, &nrounds);
           }
        } else {
           if (currnt_player == player1) {
              human_turn(br, currnt_player, &nrounds);
           } else {
              human_turn(br, currnt_player, &nrounds);
           }
        }

        currnt_player = player_next(currnt_player, player1, player2);
        simulate(br);
        winner = game_winner(br, player1, player2);
        ended = game_ended(winner, nrounds);

        if (ended) {
           ++nreplays;
           if (winner == player1) {
              ++wplayer1;
           } else if (winner == player2) {
              ++wplayer2;
           } else {
              ++nstalemates;
           }
           rover_stats(winner, player1, player2, wplayer1,
                       wplayer2, nreplays, nstalemates, mode);
           if (restart()) {
              if (winner == CROSS) {
                 if (player1 == CROSS) {
                    currnt_player = player1;
                 } else {
                    currnt_player = player2;
                 }
              } else if (winner == NOUGHT) {
                 if (player1 == NOUGHT) {
                    player1 = CROSS;
                    player2 = NOUGHT;
                    currnt_player = player1;
                 } else {
                    player1 = NOUGHT;
                    player2 = CROSS;
                    currnt_player = player2;
                 }
              } else {
                 if (player1 == CROSS) {
                    currnt_player = player1;
                 } else {
                    currnt_player = player2;
                 }
              }
              nround_announce(winner, player1, player2, currnt_player);
              nrounds = 0;
              winner = NO_MATCH;
              ended = false;
           } else {
              gover_stats(wplayer1, wplayer2, nreplays, nstalemates, mode);
           }
        }
     }
     return 0;
}
示例#6
0
/*
 * adjudicate
 *
 * Usage: adjudicate white_player black_player result
 *
 *   Adjudicates a saved (stored) game between white_player and black_player.
 *   The result is one of: abort, draw, white, black.  "Abort" cancels the game
 *   (no win, loss or draw), "white" gives white_player the win, "black" gives
 *   black_player the win, and "draw" gives a draw.
 */
int com_adjudicate(int p, param_list param)
{
  int wp, wconnected, bp, bconnected, g, inprogress, confused = 0;

  if (!FindPlayer(p, param[0].val.word, &wp, &wconnected))
    return COM_OK;
  if (!FindPlayer(p, param[1].val.word, &bp, &bconnected)) {
    if (!wconnected)
     player_remove(wp);
    return COM_OK;
  }

  inprogress = ((player_globals.parray[wp].game >=0) &&(player_globals.parray[wp].opponent == bp));

  if (inprogress) {
    g = player_globals.parray[wp].game;
  } else {
    g = game_new();
    if (game_read(g, wp, bp) < 0) {
      confused = 1;
      pprintf(p, "There is no stored game %s vs. %s\n", player_globals.parray[wp].name, player_globals.parray[bp].name);
    } else {
      game_globals.garray[g].white = wp;
      game_globals.garray[g].black = bp;
    }
  }
  if (!confused) {
    if (strstr("abort", param[2].val.word) != NULL) {
      game_ended(g, WHITE, END_ADJABORT);

      pcommand(p, "message %s Your game \"%s vs. %s\" has been aborted.",
	       player_globals.parray[wp].name, player_globals.parray[wp].name, player_globals.parray[bp].name);

      pcommand(p, "message %s Your game \"%s vs. %s\" has been aborted.",
	       player_globals.parray[bp].name, player_globals.parray[wp].name, player_globals.parray[bp].name);
    } else if (strstr("draw", param[2].val.word) != NULL) {
      game_ended(g, WHITE, END_ADJDRAW);

      pcommand(p, "message %s Your game \"%s vs. %s\" has been adjudicated "
	       "as a draw", player_globals.parray[wp].name, player_globals.parray[wp].name, player_globals.parray[bp].name);

      pcommand(p, "message %s Your game \"%s vs. %s\" has been adjudicated "
	       "as a draw", player_globals.parray[bp].name, player_globals.parray[wp].name, player_globals.parray[bp].name);
    } else if (strstr("white", param[2].val.word) != NULL) {
      game_ended(g, WHITE, END_ADJWIN);

      pcommand(p, "message %s Your game \"%s vs. %s\" has been adjudicated "
	       "as a win", player_globals.parray[wp].name, player_globals.parray[wp].name, player_globals.parray[bp].name);

      pcommand(p, "message %s Your game \"%s vs. %s\" has been adjudicated "
	       "as a loss", player_globals.parray[bp].name, player_globals.parray[wp].name, player_globals.parray[bp].name);
    } else if (strstr("black", param[2].val.word) != NULL) {
      game_ended(g, BLACK, END_ADJWIN);
      pcommand(p, "message %s Your game \"%s vs. %s\" has been adjudicated "
	       "as a loss", player_globals.parray[wp].name, player_globals.parray[wp].name, player_globals.parray[bp].name);

      pcommand(p, "message %s Your game \"%s vs. %s\" has been adjudicated "
	       "as a win", player_globals.parray[bp].name, player_globals.parray[wp].name, player_globals.parray[bp].name);
    } else {
      confused = 1;
      pprintf(p, "Result must be one of: abort draw white black\n");
    }
  }
  if (!confused) {
    pprintf(p, "Game adjudicated.\n");
    if (!inprogress) {
      game_delete(wp, bp);
    } else {
      return (COM_OK);
    }
  }
  game_remove(g);
  if (!wconnected)
    player_remove(wp);
  if (!bconnected)
    player_remove(bp);
  return COM_OK;
}
示例#7
0
void
net_replay(void)
{
    char buf[BUFSZ];
    struct nhnet_game *gamelist;
    struct nh_menulist menu;
    int pick[1];
    int i, gamecount, gameid, want_done, show_all;

    want_done = FALSE;
    show_all = TRUE;
    while (1) {
        gamelist = nhnet_list_games(want_done, show_all, &gamecount);

        init_menulist(&menu);

        if (!gamecount)
            add_menu_txt(&menu, "(No games in this list)",
                         MI_NORMAL);

        /* add all the files to the menu */
        for (i = 0; i < gamecount; i++) {
            describe_game(buf, gamelist[i].status, &gamelist[i].i);
            add_menu_item(&menu, gamelist[i].gameid, buf, 0,
                          FALSE);
        }

        add_menu_txt(&menu, "", MI_NORMAL);
        if (want_done)
            add_menu_item(&menu, -1, show_all ? "Watch current games" :
                          "Replay your unfinished games", '!', FALSE);
        else
            add_menu_item(&menu, -1,
                          "Replay a completed game", '!', FALSE);

        if (show_all)
            add_menu_item(&menu, -2, want_done ? "View your completed games" :
                          "View your saved games", '#', FALSE);
        else
            add_menu_item(&menu, -2, "View games from other players",
                          '#', FALSE);

        curses_display_menu(&menu, want_done ? show_all ?
                            "Completed games by other players" :
                            "Your completed games" : show_all ?
                            "Pick a current game to watch" :
                            "Replay your saved games", PICK_ONE,
                            PLHINT_ANYWHERE, pick, curses_menu_callback);
        if (pick[0] == CURSES_MENU_CANCELLED)
            return;

        if (pick[0] == -1) {
            want_done = !want_done;
            if (want_done)
                show_all = FALSE; /* will normally be intended */
            continue;
        } else if (pick[0] == -2) {
            show_all = !show_all;
            continue;
        } else
            gameid = pick[0];

        break;
    }

    create_game_windows();

    /* If the game is over, we want to replay (nothing to watch). If it's our
       game, we want to replay (not much point in watching yourself). Otherwise,
       we start in watch mode; the player can change to replay mode from the
       watch menu. */
    int ret = playgame(gameid, show_all && !want_done ? FM_WATCH : FM_REPLAY);

    destroy_game_windows();
    cleanup_messages();

    game_ended(ret, NULL, TRUE);
}
示例#8
0
nh_bool loadgame(void)
{
    char buf[BUFSZ];
    fnchar savedir[BUFSZ], filename[1024], **files;
    struct nh_menuitem *items;
    int size, icount, fd, i, n, ret, pick[1];
    enum nh_log_status status;
    struct nh_game_info gi;

    if (!get_gamedir(SAVE_DIR, savedir)) {
        curses_raw_print("Could not find or create the save directory.");
        return FALSE;
    }

    files = list_gamefiles(savedir, &size);
    if (!size) {
        curses_msgwin("No saved games found.");
        return FALSE;
    }

    icount = 0;
    items = malloc(size * sizeof(struct nh_menuitem));

    for (i = 0; i < size; i++) {
        fd = sys_open(files[i], O_RDWR, FILE_OPEN_MASK);
        status = nh_get_savegame_status(fd, &gi);
        close(fd);

        describe_game(buf, status, &gi);
        add_menu_item(items, size, icount, (status == LS_IN_PROGRESS) ? 0 : icount + 1,
                      buf, 0, FALSE);
    }

    n = curses_display_menu(items, icount, "saved games", PICK_ONE, pick);
    free(items);
    filename[0] = '\0';
    if (n > 0)
        fnncat(filename, files[pick[0]-1], sizeof(filename)/sizeof(fnchar)-1);

    for (i = 0; i < icount; i++)
        free(files[i]);
    free(files);
    if (n <= 0)
        return FALSE;

    fd = sys_open(filename, O_RDWR, FILE_OPEN_MASK);
    create_game_windows();
    if (nh_restore_game(fd, NULL, FALSE) != GAME_RESTORED) {
        destroy_game_windows();
        close(fd);
        if (curses_yn_function("Failed to load the save. Do you wish to delete the file?", "yn", 'n') == 'y')
            unlink(filename);
        return FALSE;
    }

    load_keymap(); /* need to load the keymap after the game has been started */
    ret = commandloop();
    free_keymap();
    close(fd);

    destroy_game_windows();
    cleanup_messages();
    game_ended(ret, filename);

    return TRUE;
}