OpenCores
URL https://opencores.org/ocsvn/connect-6/connect-6/trunk

Subversion Repositories connect-6

[/] [connect-6/] [trunk/] [CONNECTK/] [connectk-2.0/] [src/] [file.c] - Blame information for rev 4

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 3 sumanta.ch
 
2
/*
3
 
4
connectk -- a program to play the connect-k family of games
5
Copyright (C) 2007 Michael Levin
6
 
7
This program is free software; you can redistribute it and/or
8
modify it under the terms of the GNU General Public License
9
as published by the Free Software Foundation; either version 2
10
of the License, or (at your option) any later version.
11
 
12
This program is distributed in the hope that it will be useful,
13
but WITHOUT ANY WARRANTY; without even the implied warranty of
14
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
GNU General Public License for more details.
16
 
17
You should have received a copy of the GNU General Public License
18
along with this program; if not, write to the Free Software
19
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20
 
21
*/
22
 
23
#include "config.h"
24
#include <glib.h>
25
#include "shared.h"
26
#include "connectk.h"
27
 
28
int load_moves_list(const char *filename)
29
{
30
        GIOChannel *ioc;
31
        GIOStatus status;
32
        GError *error = NULL;
33
        gchar *line = NULL;
34
        gsize len, term;
35
        int size = 0;
36
 
37
        ioc = g_io_channel_new_file(filename, "r", &error);
38
        if (!ioc) {
39
                g_warning("Failed to open file '%s' for reading: %s", filename,
40
                          error->message);
41
                return 0;
42
        }
43
 
44
        connect_k = 0;
45
        place_p = 0;
46
        start_q = 0;
47
 
48
        /* Read the first non-blank line */
49
        do {
50
                g_free(line);
51
                status = g_io_channel_read_line(ioc, &line, &len, &term,
52
                                                &error);
53
                if (error) {
54
                        g_warning("Error reading from file '%s': %s",
55
                                  filename, error->message);
56
                        return 0;
57
                }
58
                if (!line)
59
                        continue;
60
                line[term] = 0;
61
                g_strchug(line);
62
        } while(status == G_IO_STATUS_NORMAL && *line == 0);
63
 
64
        /* Read "size k p q" header */
65
        if (g_ascii_isdigit(*line)) {
66
                size = g_ascii_strtoull(line, &line, 10);
67
                g_strchug(line);
68
                connect_k = g_ascii_strtoull(line, &line, 10);
69
                g_strchug(line);
70
                place_p = g_ascii_strtoull(line, &line, 10);
71
                g_strchug(line);
72
                start_q = g_ascii_strtoull(line, &line, 10);
73
                g_strchug(line);
74
                /* Game victor is skipped */
75
        }
76
 
77
        /* Default to connect-6 */
78
        if (!size)
79
                size = 19;
80
        if (!connect_k)
81
                connect_k = 6;
82
        if (!place_p)
83
                place_p = 2;
84
        if (!start_q)
85
                start_q = 1;
86
        new_game(size);
87
 
88
        /* Read moves list in Ba1 format */
89
        while (g_io_channel_read_line(ioc, &line, &len, &term, &error) ==
90
               G_IO_STATUS_NORMAL) {
91
                BCOORD x, y;
92
 
93
                if (!line)
94
                        continue;
95
                line[term] = 0;
96
                g_strchug(line);
97
                string_to_bcoords(line, &x, &y);
98
                make_move(x, y);
99
                g_free(line);
100
        }
101
 
102
        g_io_channel_unref(ioc);
103
        return 1;
104
}
105
 
106
int save_moves_list(const char *filename)
107
{
108
        GIOChannel *ioc;
109
        GError *error = NULL;
110
        Board *last_b;
111
        PIECE winner = PIECE_NONE;
112
        int i;
113
        char *buf;
114
 
115
        ioc = g_io_channel_new_file(filename, "w", &error);
116
        if (!ioc) {
117
                g_warning("Failed to open file '%s' for writing: %s", filename,
118
                          error->message);
119
                return 0;
120
        }
121
 
122
        /* Find the winner */
123
        last_b = board_at(move_last);
124
        if (last_b && last_b->won)
125
                winner = last_b->turn;
126
 
127
        buf = va("%d %d %d %d %d\n", board_size, connect_k, place_p, start_q,
128
                 winner);
129
        g_io_channel_write_chars(ioc, buf, -1, NULL, &error);
130
        if (error) {
131
                g_warning("Error writing to file '%s': %s",
132
                          filename, error->message);
133
                return 0;
134
        }
135
        for (i = 0; i < move_last; i++) {
136
                Board *b;
137
 
138
                b = board_at(i);
139
                if (b->won)
140
                        break;
141
                buf = va("%s\n", bcoords_to_string(b->move_x, b->move_y));
142
                g_io_channel_write_chars(ioc, buf, -1, NULL, &error);
143
                if (error) {
144
                        g_warning("Error writing to file '%s': %s",
145
                                  filename, error->message);
146
                        return 0;
147
                }
148
        }
149
        g_io_channel_unref(ioc);
150
        return 1;
151
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.