forked from PascalPons/connect4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
93 lines (80 loc) · 2.63 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
* This file is part of Connect4 Game Solver <http://connect4.gamesolver.org>
* Copyright (C) 2007 Pascal Pons <[email protected]>
*
* Connect4 Game Solver is free software: you can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* Connect4 Game Solver is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Connect4 Game Solver. If not, see <http://www.gnu.org/licenses/>.
*/
#include "solver.hpp"
#include <iostream>
#include <fstream>
#include <sys/time.h>
#include <cstdio>
using namespace GameSolver::Connect4;
/*
* Get micro-second precision timestamp
* uses unix gettimeofday function
*/
unsigned long long getTimeMicrosec() {
timeval NOW;
gettimeofday(&NOW, NULL);
return NOW.tv_sec*1000000LL + NOW.tv_usec;
}
/*
* Main function.
* Reads Connect 4 positions, line by line, from standard input
* and writes one line per position to standard output containing:
* - score of the position
* - number of nodes explored
* - time spent in microsecond to solve the position.
*
* Any invalid position (invalid sequence of move, or already won game)
* will generate an error message to standard error and an empty line to standard output.
*/
int solve(Solver &solver, std::string line);
int main() {
Solver solver;
std::string lock = "/Users/Arpit/DDQN/src/games/lock";
std::string outputFile = "/Users/Arpit/DDQN/src/games/out.txt";
std::string line;
std::string output = "";
for(int l = 1; std::getline(std::cin, line); l++) {
//unsigned long long start_time = getTimeMicrosec();
output = "";
std::ofstream outfile(lock); outfile.close();
for (int j=1; j<=Position::WIDTH; j++){
output.append(std::to_string(solve(solver, line + std::to_string(j))));
output.append(" ");
}
std::ofstream myfile;
myfile.open(outputFile);
myfile << output;
myfile.close();
std::remove("/Users/Arpit/DDQN/src/games/lock");
//std::cout<<output<<" "<<getTimeMicrosec() - start_time<<std::endl;
}
exit(0);
}
int solve(Solver &solver, std::string line) {
Position P;
int score = 0;
int result = P.play(line);
if(result == -1) {//invalid move
score = 99;
}else if (result == -2) {//has won already
score = -99;
}else {
score = solver.solve(P, false);
}
return score;
}