|
|
/*
|
/*
|
|
|
connectk -- a program to play the connect-k family of games
|
connectk -- a program to play the connect-k family of games
|
Copyright (C) 2007 Michael Levin
|
Copyright (C) 2007 Michael Levin
|
|
|
This program is free software; you can redistribute it and/or
|
This program is free software; you can redistribute it and/or
|
modify it under the terms of the GNU General Public License
|
modify it under the terms of the GNU General Public License
|
as published by the Free Software Foundation; either version 2
|
as published by the Free Software Foundation; either version 2
|
of the License, or (at your option) any later version.
|
of the License, or (at your option) any later version.
|
|
|
This program is distributed in the hope that it will be useful,
|
This program is distributed in the hope that it will be useful,
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
GNU General Public License for more details.
|
GNU General Public License for more details.
|
|
|
You should have received a copy of the GNU General Public License
|
You should have received a copy of the GNU General Public License
|
along with this program; if not, write to the Free Software
|
along with this program; if not, write to the Free Software
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
*/
|
*/
|
|
|
//#include "config.h"
|
//#include "config.h"
|
//#include <math.h>
|
//#include <math.h>
|
//#include <glib->h>
|
//#include <glib->h>
|
//#include <iostream>
|
//#include <iostream>
|
#include "./shared.h"
|
#include "./shared.h"
|
//#include "./q.hpp"
|
//#include "./q.hpp"
|
#include "pico.h"
|
#include "pico.h"
|
//#include <stdio.h>
|
//#include <stdio.h>
|
|
|
/* Bits per threat level */
|
/* Bits per threat level */
|
#define BITS_PER_THREAT 6
|
#define BITS_PER_THREAT 6
|
|
|
//FIFO(x,int);
|
//FIFO(x,int);
|
//#pragma fifo_length x 24
|
//#pragma fifo_length x 24
|
//#pragma no_inter_loop_stream_analysis pico_stream_input_x
|
//#pragma no_inter_loop_stream_analysis pico_stream_input_x
|
//#pragma no_inter_loop_stream_analysis pico_stream_output_x
|
//#pragma no_inter_loop_stream_analysis pico_stream_output_x
|
//#pragma no_inter_task_stream_analysis pico_stream_input_x
|
//#pragma no_inter_task_stream_analysis pico_stream_input_x
|
//#pragma no_inter_task_stream_analysis pico_stream_output_x
|
//#pragma no_inter_task_stream_analysis pico_stream_output_x
|
/*--------------------------------------------------------------------*/
|
/*--------------------------------------------------------------------*/
|
#ifdef PICO_SYNTH
|
#ifdef PICO_SYNTH
|
#define Q_ASSERT(_cond, _msg)
|
#define Q_ASSERT(_cond, _msg)
|
//#include <iostream>
|
//#include <iostream>
|
//#include "pico.h"
|
//#include "pico.h"
|
//#include "q.hpp"
|
//#include "q.hpp"
|
//#include "./shared.h"
|
//#include "./shared.h"
|
using namespace std;
|
using namespace std;
|
#else
|
#else
|
/* not synthesizable */
|
/* not synthesizable */
|
#include <iostream>
|
#include <iostream>
|
#include <sstream>
|
#include <sstream>
|
#include <string>
|
#include <string>
|
#include <assert.h>
|
#include <assert.h>
|
|
|
static void debug_assert (bool cond, char * msg) {
|
static void debug_assert (bool cond, char * msg) {
|
if (!cond) {
|
if (!cond) {
|
printf("assert failed: %s\n", msg);
|
printf("assert failed: %s\n", msg);
|
assert(0);
|
assert(0);
|
}
|
}
|
}
|
}
|
|
|
#define Q_ASSERT(_cond, _msg) debug_assert(_cond, _msg)
|
#define Q_ASSERT(_cond, _msg) debug_assert(_cond, _msg)
|
#endif
|
#endif
|
#define max_size 361
|
#define max_size 361
|
#define ptr_bw 32
|
#define ptr_bw 32
|
//FIFO(queue,AIMove);
|
//FIFO(queue,AIMove);
|
//#pragma no_inter_loop_stream_analysis pico_stream_input_queue
|
//#pragma no_inter_loop_stream_analysis pico_stream_input_queue
|
//#pragma no_inter_loop_stream_analysis pico_stream_output_queue
|
//#pragma no_inter_loop_stream_analysis pico_stream_output_queue
|
//#pragma no_inter_task_stream_analysis pico_stream_input_queue
|
//#pragma no_inter_task_stream_analysis pico_stream_input_queue
|
//#pragma no_inter_task_stream_analysis pico_stream_output_queue
|
//#pragma no_inter_task_stream_analysis pico_stream_output_queue
|
|
|
//FIFO(queue,AIMove);
|
//FIFO(queue,AIMove);
|
//FIFO_INTERFACE(queue,AIMove);
|
//FIFO_INTERFACE(queue,AIMove);
|
//#pragma fifo_length pico_stream_input_queue 800
|
//#pragma fifo_length pico_stream_input_queue 800
|
//#pragma fifo_length pico_stream_output_queue 800
|
//#pragma fifo_length pico_stream_output_queue 800
|
//#pragma read_write_port queue separate
|
//#pragma read_write_port queue separate
|
//#pragma bandwidth pico_stream_input_queue 1
|
//#pragma bandwidth pico_stream_input_queue 1
|
//#pragma bandwidth pico_stream_output_queue 1
|
//#pragma bandwidth pico_stream_output_queue 1
|
//////template <class tp=AIMove, int max_size=128, int ptr_bw=32>
|
//////template <class tp=AIMove, int max_size=128, int ptr_bw=32>
|
////
|
////
|
//// /* pop front of queue, returning the front data */
|
//// /* pop front of queue, returning the front data */
|
//// /* q is corrupted if pop when empty */
|
//// /* q is corrupted if pop when empty */
|
//// AIMove q::pop (){
|
//// AIMove q::pop (){
|
//// /* assert that before pop, queue is not empty (underflow check) */
|
//// /* assert that before pop, queue is not empty (underflow check) */
|
//// Q_ASSERT((!wrapped && (head < tail)) || (wrapped && (head >= tail)),
|
//// Q_ASSERT((!wrapped && (head < tail)) || (wrapped && (head >= tail)),
|
//// "queue underflowed");
|
//// "queue underflowed");
|
//// AIMove d = pico_stream_input_queue();
|
//// AIMove d = pico_stream_input_queue();
|
//// //cout <<"pop: "<<head<<":"<<tail<<":"<<wrapped<<endl;
|
//// //cout <<"pop: "<<head<<":"<<tail<<":"<<wrapped<<endl;
|
//// if (head == max_size-1) {
|
//// if (head == max_size-1) {
|
//// head = 0;
|
//// head = 0;
|
//// wrapped = false;
|
//// wrapped = false;
|
//// } else {
|
//// } else {
|
//// head = head + 1;
|
//// head = head + 1;
|
//// }
|
//// }
|
//// return d;
|
//// return d;
|
//// }
|
//// }
|
////
|
////
|
//// /* push data into back of queue */
|
//// /* push data into back of queue */
|
//// /* q is corrupted if push when full */
|
//// /* q is corrupted if push when full */
|
//// void q::push (AIMove d){
|
//// void q::push (AIMove d){
|
//// pico_stream_output_queue(d);
|
//// pico_stream_output_queue(d);
|
//// if (tail == max_size-1) {
|
//// if (tail == max_size-1) {
|
//// tail = 0;
|
//// tail = 0;
|
//// wrapped = true;
|
//// wrapped = true;
|
//// } else {
|
//// } else {
|
//// tail = tail + 1;
|
//// tail = tail + 1;
|
//// }
|
//// }
|
//// /* assert that after push, queue is not empty (overflow check) */
|
//// /* assert that after push, queue is not empty (overflow check) */
|
//// Q_ASSERT((!wrapped && (head < tail)) || (wrapped && (head >= tail)),
|
//// Q_ASSERT((!wrapped && (head < tail)) || (wrapped && (head >= tail)),
|
//// "Queue overflowed") ;
|
//// "Queue overflowed") ;
|
//// //cout <<"push: "<<head<<":"<<tail<<":"<<wrapped<<endl;
|
//// //cout <<"push: "<<head<<":"<<tail<<":"<<wrapped<<endl;
|
//// }
|
//// }
|
////
|
////
|
//// /* return current size of the queue */
|
//// /* return current size of the queue */
|
//// int q::size (){
|
//// int q::size (){
|
//// if (wrapped) {
|
//// if (wrapped) {
|
//// return (max_size - head) + (tail - 0);
|
//// return (max_size - head) + (tail - 0);
|
//// } else {
|
//// } else {
|
//// return tail - head;
|
//// return tail - head;
|
//// }
|
//// }
|
//// }
|
//// }
|
/////*--------------------------------------------------------------------*/
|
/////*--------------------------------------------------------------------*/
|
////void test(int ready){
|
////void test(int ready){
|
//// int i;
|
//// int i;
|
//// //for (i=0;i<10;i++) if(ready>1) pico_stream_input_x();
|
//// //for (i=0;i<10;i++) if(ready>1) pico_stream_input_x();
|
//// //for (i=0;i<10;i++) {if(!moves_fifo1.empty()) moves_fifo1.pop(); }
|
//// //for (i=0;i<10;i++) {if(!moves_fifo1.empty()) moves_fifo1.pop(); }
|
//// AIMove m;
|
//// AIMove m;
|
//// while(1) {
|
//// while(1) {
|
//// if(ready>1) m=pico_stream_input_queue();
|
//// if(ready>1) m=pico_stream_input_queue();
|
//// if(m.weight==-1) break;
|
//// if(m.weight==-1) break;
|
//// }
|
//// }
|
////}
|
////}
|
////q moves_fifo1;
|
////q moves_fifo1;
|
|
|
static AIWEIGHT threat_bits(int threat, PIECE type, Board *b)
|
static AIWEIGHT threat_bits(int threat, PIECE type, Board *b)
|
/* Bit pack the threat value */
|
/* Bit pack the threat value */
|
{
|
{
|
if (threat < 1)
|
if (threat < 1)
|
return 0;
|
return 0;
|
|
|
/* No extra value for building sequences over k - p unless it is
|
/* No extra value for building sequences over k - p unless it is
|
enough to win */
|
enough to win */
|
if (b->turn == type && connect_k - threat <= b->moves_left)
|
if (b->turn == type && connect_k - threat <= b->moves_left)
|
threat = connect_k - place_p + 1;
|
threat = connect_k - place_p + 1;
|
else if (threat >= connect_k - place_p)
|
else if (threat >= connect_k - place_p)
|
threat = connect_k - place_p - (type == b->turn);
|
threat = connect_k - place_p - (type == b->turn);
|
|
|
return 1 << ((threat - 1) * BITS_PER_THREAT);
|
return 1 << ((threat - 1) * BITS_PER_THREAT);
|
}
|
}
|
|
|
static void threat_mark(int i, int threat, PIECE type,Board *b,Line *line)
|
static void threat_mark(int i, int threat, PIECE type,Board *b,Line *line)
|
{
|
{
|
int j, index = 0;
|
int j, index = 0;
|
|
|
if (threat <= 0)
|
if (threat <= 0)
|
return;
|
return;
|
|
|
/* No extra value for building sequences over k - p unless it is
|
/* No extra value for building sequences over k - p unless it is
|
enough to win */
|
enough to win */
|
if (b->turn == type && connect_k - threat <= b->moves_left)
|
if (b->turn == type && connect_k - threat <= b->moves_left)
|
threat = connect_k - place_p + 1;
|
threat = connect_k - place_p + 1;
|
else if (threat >= connect_k - place_p)
|
else if (threat >= connect_k - place_p)
|
threat = connect_k - place_p - (type == b->turn);
|
threat = connect_k - place_p - (type == b->turn);
|
|
|
/* Do not mark if this threat is dominated by a preceeding threat;
|
/* Do not mark if this threat is dominated by a preceeding threat;
|
Likewise supress any smaller threats */
|
Likewise supress any smaller threats */
|
for (j = i; j >= 0 && j > i - connect_k; j--)
|
for (j = i; j >= 0 && j > i - connect_k; j--)
|
if (line[j].threat[0] > threat)
|
if (line[j].threat[0] > threat)
|
return;
|
return;
|
else if (line[j].threat[0] < threat) {
|
else if (line[j].threat[0] < threat) {
|
line[j].threat[0] = 0;
|
line[j].threat[0] = 0;
|
line[j].threat[1] = 0;
|
line[j].threat[1] = 0;
|
}
|
}
|
|
|
/* Store up to two threats per tile in the line */
|
/* Store up to two threats per tile in the line */
|
if (line[i].threat[index])
|
if (line[i].threat[index])
|
index++;
|
index++;
|
line[i].threat[index] = threat;
|
line[i].threat[index] = threat;
|
line[i].turn[index] = type;
|
line[i].turn[index] = type;
|
}
|
}
|
|
|
int threat_window(int x, int y, int dx, int dy,
|
int threat_window(int x, int y, int dx, int dy,
|
PIECE *ptype, int *pdouble,Board *b)
|
PIECE *ptype, int *pdouble,Board *b)
|
{
|
{
|
int minimum, maximum, count = 0;
|
int minimum, maximum, count = 0;
|
PIECE p, type = PIECE_NONE;
|
PIECE p, type = PIECE_NONE;
|
|
|
/* Check if this tile is empty */
|
/* Check if this tile is empty */
|
p = piece_at(b, x, y);
|
p = piece_at(b, x, y);
|
if (!piece_empty(p))
|
if (!piece_empty(p))
|
return 0;
|
return 0;
|
|
|
/* Push forward the maximum and find the window type */
|
/* Push forward the maximum and find the window type */
|
#pragma unroll
|
#pragma unroll
|
#pragma num_iterations(1,3,6)
|
#pragma num_iterations(1,3,6)
|
for (maximum = 1; maximum < connect_k; maximum++) {
|
for (maximum = 1; maximum < connect_k; maximum++) {
|
p = piece_at(b, x + dx * maximum, y + dy * maximum);
|
p = piece_at(b, x + dx * maximum, y + dy * maximum);
|
if (p == PIECE_ERROR)
|
if (p == PIECE_ERROR)
|
break;
|
break;
|
if (!piece_empty(p)) {
|
if (!piece_empty(p)) {
|
if (type == PIECE_NONE)
|
if (type == PIECE_NONE)
|
type = p;
|
type = p;
|
else if (type != p)
|
else if (type != p)
|
break;
|
break;
|
count++;
|
count++;
|
}
|
}
|
}
|
}
|
maximum--;
|
maximum--;
|
|
|
/* Try to push the entire window back */
|
/* Try to push the entire window back */
|
#pragma unroll
|
#pragma unroll
|
#pragma num_iterations(1,3,6)
|
#pragma num_iterations(1,3,6)
|
for (minimum = -1; minimum > -connect_k; minimum--) {
|
for (minimum = -1; minimum > -connect_k; minimum--) {
|
p = piece_at(b, x + dx * minimum, y + dy * minimum);
|
p = piece_at(b, x + dx * minimum, y + dy * minimum);
|
if (p == PIECE_ERROR || piece_empty(p))
|
if (p == PIECE_ERROR || piece_empty(p))
|
break;
|
break;
|
if (type == PIECE_NONE)
|
if (type == PIECE_NONE)
|
type = p;
|
type = p;
|
else if (type != p)
|
else if (type != p)
|
break;
|
break;
|
if (maximum - minimum > connect_k - 1) {
|
if (maximum - minimum > connect_k - 1) {
|
p = piece_at(b, x + dx * maximum, y + dy * maximum);
|
p = piece_at(b, x + dx * maximum, y + dy * maximum);
|
if (p == type)
|
if (p == type)
|
count--;
|
count--;
|
maximum--;
|
maximum--;
|
}
|
}
|
count++;
|
count++;
|
}
|
}
|
minimum++;
|
minimum++;
|
|
|
/* Push back minimum if we haven't formed a complete window, this window
|
/* Push back minimum if we haven't formed a complete window, this window
|
can't be a double */
|
can't be a double */
|
if (maximum - minimum < connect_k - 1) {
|
if (maximum - minimum < connect_k - 1) {
|
//#pragma unroll
|
//#pragma unroll
|
#pragma num_iterations(1,3,6)
|
#pragma num_iterations(1,3,6)
|
for (minimum--; minimum > maximum - connect_k; minimum--) {
|
for (minimum--; minimum > maximum - connect_k; minimum--) {
|
p = piece_at(b, x + dx * minimum, y + dy * minimum);
|
p = piece_at(b, x + dx * minimum, y + dy * minimum);
|
if (p == PIECE_ERROR)
|
if (p == PIECE_ERROR)
|
break;
|
break;
|
if (!piece_empty(p)) {
|
if (!piece_empty(p)) {
|
if (type != p)
|
if (type != p)
|
break;
|
break;
|
if (type == PIECE_NONE)
|
if (type == PIECE_NONE)
|
type = p;
|
type = p;
|
count++;
|
count++;
|
}
|
}
|
}
|
}
|
*pdouble = 0;
|
*pdouble = 0;
|
minimum++;
|
minimum++;
|
}
|
}
|
|
|
*ptype = type;
|
*ptype = type;
|
if (maximum - minimum >= connect_k - 1)
|
if (maximum - minimum >= connect_k - 1)
|
return count;
|
return count;
|
return 0;
|
return 0;
|
}
|
}
|
|
|
/*static*/ AIWEIGHT threat_line(int x, int y, int dx, int dy,Board *b,Board *bwrite,int k,int loop_bound)
|
/*static*/ AIWEIGHT threat_line(int x, int y, int dx, int dy,Board *b,Board *bwrite,int k,int loop_bound)
|
{
|
{
|
|
|
//#pragma read_write_ports threat_counts.data combined 2
|
//#pragma read_write_ports threat_counts.data combined 2
|
//#pragma internal_blockram threat_counts
|
//#pragma internal_blockram threat_counts
|
//#pragma no_memory_analysis threat_counts
|
//#pragma no_memory_analysis threat_counts
|
|
|
//#pragma read_write_ports b.data combined 2
|
//#pragma read_write_ports b.data combined 2
|
//#pragma read_write_ports b.data separate 1 readonly 2 writeonly
|
//#pragma read_write_ports b.data separate 1 readonly 2 writeonly
|
//#pragma no_memory_analysis b
|
//#pragma no_memory_analysis b
|
/* This is the line of threats currently being processed */
|
/* This is the line of threats currently being processed */
|
Line line[board_size]={{1},{2}};
|
Line line[board_size]={{1},{2}};
|
#pragma internal_fast line
|
#pragma internal_fast line
|
//#pragma no_memory_analysis line
|
//#pragma no_memory_analysis line
|
/* Running tally of threats for both players */
|
/* Running tally of threats for both players */
|
//static int threat_counts[MAX_CONNECT_K + 1][2];
|
//static int threat_counts[MAX_CONNECT_K + 1][2];
|
threat_count_array threat_counts={{0}};
|
threat_count_array threat_counts={{0}};
|
#pragma internal_fast threat_counts
|
#pragma internal_fast threat_counts
|
//#pragma read_write_ports threat_counts.data combined 2
|
//#pragma read_write_ports threat_counts.data combined 2
|
//#pragma no_memory_analysis threat_counts
|
//#pragma no_memory_analysis threat_counts
|
//if (k==1) board_copy(b, bwrite);
|
//if (k==1) board_copy(b, bwrite);
|
int i;
|
int i;
|
AIWEIGHT weight = 0;
|
AIWEIGHT weight = 0;
|
///* Clear threat tallys */
|
///* Clear threat tallys */
|
//for (i = 0; i < connect_k; i++) {
|
//for (i = 0; i < connect_k; i++) {
|
// threat_counts.data[i][0] = 1;
|
// threat_counts.data[i][0] = 1;
|
// threat_counts.data[i][1] = 1;
|
// threat_counts.data[i][1] = 1;
|
//}
|
//}
|
|
|
/* Mark the maximum threat for each */
|
/* Mark the maximum threat for each */
|
for (i = 0; x >= 0 && x < board_size && y >= 0 && y < board_size; i++) {
|
for (i = 0; x >= 0 && x < board_size && y >= 0 && y < board_size; i++) {
|
int count[2], tmp, double_threat = 1;
|
int count[2], tmp, double_threat = 1;
|
PIECE type[2];
|
PIECE type[2];
|
|
|
count[0] = threat_window(x, y, dx, dy, type, &double_threat,bwrite);
|
count[0] = threat_window(x, y, dx, dy, type, &double_threat,bwrite);
|
count[1] = threat_window(x, y, -dx, -dy, type + 1,
|
count[1] = threat_window(x, y, -dx, -dy, type + 1,
|
&double_threat,bwrite);
|
&double_threat,bwrite);
|
if (count[1] > count[0]) {
|
if (count[1] > count[0]) {
|
tmp = count[1];
|
tmp = count[1];
|
count[1] = count[0];
|
count[1] = count[0];
|
count[0] = tmp;
|
count[0] = tmp;
|
tmp = type[1];
|
tmp = type[1];
|
type[1] = type[0];
|
type[1] = type[0];
|
type[0] = tmp;
|
type[0] = tmp;
|
}
|
}
|
line[i].threat[0] = 0;
|
line[i].threat[0] = 0;
|
line[i].threat[1] = 0;
|
line[i].threat[1] = 0;
|
threat_mark(i, count[0], type[0],bwrite,&line[0]);
|
threat_mark(i, count[0], type[0],bwrite,&line[0]);
|
if (double_threat)
|
if (double_threat)
|
threat_mark(i, count[1], type[1],bwrite,&line[0]);
|
threat_mark(i, count[1], type[1],bwrite,&line[0]);
|
x += dx;
|
x += dx;
|
y += dy;
|
y += dy;
|
}
|
}
|
|
|
/* Commit stored line values to the board */
|
/* Commit stored line values to the board */
|
x -= dx;
|
x -= dx;
|
y -= dy;
|
y -= dy;
|
for (i--; x >= 0 && x < board_size && y >= 0 && y < board_size; i--) {
|
for (i--; x >= 0 && x < board_size && y >= 0 && y < board_size; i--) {
|
AIWEIGHT bits[2];
|
AIWEIGHT bits[2];
|
PIECE p;
|
PIECE p;
|
|
|
bits[0] = threat_bits(line[i].threat[0], line[i].turn[0],bwrite);
|
bits[0] = threat_bits(line[i].threat[0], line[i].turn[0],bwrite);
|
bits[1] = threat_bits(line[i].threat[1], line[i].turn[1],bwrite);
|
bits[1] = threat_bits(line[i].threat[1], line[i].turn[1],bwrite);
|
p = piece_at(bwrite, x, y);
|
p = piece_at(bwrite, x, y);
|
if (piece_empty(p) && line[i].threat[0]) {
|
if (piece_empty(p) && line[i].threat[0]) {
|
threat_counts.data[line[i].threat[0]][line[i].turn[0] - 1]++;
|
threat_counts.data[line[i].threat[0]][line[i].turn[0] - 1]++;
|
if (line[i].threat[1])
|
if (line[i].threat[1])
|
threat_counts.data[line[i].threat[1]]
|
threat_counts.data[line[i].threat[1]]
|
[line[i].turn[1] - 1]++;
|
[line[i].turn[1] - 1]++;
|
if (p >= PIECE_THREAT0)
|
if (p >= PIECE_THREAT0)
|
place_threat(bwrite, x, y, p - PIECE_THREAT0 +
|
place_threat(bwrite, x, y, p - PIECE_THREAT0 +
|
bits[0] + bits[1]);
|
bits[0] + bits[1]);
|
else
|
else
|
place_threat(bwrite, x, y, bits[0] + bits[1]);
|
place_threat(bwrite, x, y, bits[0] + bits[1]);
|
}
|
}
|
if (bwrite->turn != line[i].turn[0])
|
if (bwrite->turn != line[i].turn[0])
|
bits[0] = -bits[0];
|
bits[0] = -bits[0];
|
if (bwrite->turn != line[i].turn[1])
|
if (bwrite->turn != line[i].turn[1])
|
bits[1] = -bits[1];
|
bits[1] = -bits[1];
|
weight += bits[0] + bits[1];
|
weight += bits[0] + bits[1];
|
x -= dx;
|
x -= dx;
|
y -= dy;
|
y -= dy;
|
}
|
}
|
return weight;
|
return weight;
|
}
|
}
|
|
|
FIFO(queue,AIMove);
|
FIFO(queue,AIMove);
|
//FIFO_INTERFACE(queue,AIMove);
|
//FIFO_INTERFACE(queue,AIMove);
|
#pragma fifo_length pico_stream_input_queue 800
|
#pragma fifo_length pico_stream_input_queue 800
|
#pragma fifo_length pico_stream_output_queue 800
|
#pragma fifo_length pico_stream_output_queue 800
|
#pragma bandwidth pico_stream_input_queue 1
|
#pragma bandwidth pico_stream_input_queue 1
|
#pragma bandwidth pico_stream_output_queue 1
|
#pragma bandwidth pico_stream_output_queue 1
|
/*AIMoves*/int ai_threats(Board *board,AIMove *move)
|
/*AIMoves*/int ai_threats(Board *board,AIMoves *moves,unsigned int *index)
|
{
|
{
|
//#pragma read_write_ports board.data combined 2
|
//#pragma read_write_ports board.data combined 2
|
//#pragma internal_blockram board
|
//#pragma internal_blockram board
|
//#pragma no_memory_analysis board
|
//#pragma no_memory_analysis board
|
|
|
//#pragma internal_blockram move
|
//#pragma internal_blockram move
|
//#pragma no_memory_analysis move
|
//#pragma no_memory_analysis move
|
|
|
/////////* All threat functions work on this board */
|
/////////* All threat functions work on this board */
|
/*static*/ Board b;//={0,0,0,0,0,0,0,0,0,0,0,{{0}}} ;//= NULL;
|
/*static*/ Board b;//={0,0,0,0,0,0,0,0,0,0,0,{{0}}} ;//= NULL;
|
//#pragma read_write_ports b.data combined 2
|
//#pragma read_write_ports b.data combined 2
|
#pragma internal_blockram b
|
#pragma internal_blockram b
|
//#pragma multi_buffer b 2
|
//#pragma multi_buffer b 2
|
//#pragma read_write_ports b.data separate 1 readonly 2 writeonly
|
//#pragma read_write_ports b.data separate 1 readonly 2 writeonly
|
//#pragma no_memory_analysis b
|
//#pragma no_memory_analysis b
|
/*static*/ Board bwrite;//={0,0,0,0,0,0,0,0,0,0,0,{{0}}} ;//= NULL;
|
/*static*/ Board bwrite;//={0,0,0,0,0,0,0,0,0,0,0,{{0}}} ;//= NULL;
|
//#pragma read_write_ports b.data combined 2
|
//#pragma read_write_ports b.data combined 2
|
#pragma internal_blockram bwrite
|
#pragma internal_blockram bwrite
|
//#pragma multi_buffer bwrite 22
|
//#pragma multi_buffer bwrite 22
|
//#pragma no_memory_analysis b
|
//#pragma no_memory_analysis b
|
/*static*/ AIMoves moves;//={0,0,0,{{0,0,0}}};
|
///*static*/ AIMoves moves;//={0,0,0,{{0,0,0}}};
|
//#pragma read_write_ports moves.data combined 3
|
//#pragma read_write_ports moves.data combined 3
|
#pragma internal_blockram moves
|
#pragma internal_blockram moves
|
//#pragma no_memory_analysis moves
|
//#pragma no_memory_analysis moves
|
|
|
moves.len=0;
|
moves->len=0;
|
//AIMoves moves;
|
//AIMoves moves;
|
AIWEIGHT u_sum = 0;
|
AIWEIGHT u_sum = 0;
|
int i;
|
int i;
|
|
|
//b = board_new();
|
//b = board_new();
|
//Board b;
|
//Board b;
|
board_copy(board, &b);
|
board_copy(board, &b);
|
|
|
/* Clear threat tallys */
|
/* Clear threat tallys */
|
//for (i = 0; i < connect_k; i++) {
|
//for (i = 0; i < connect_k; i++) {
|
// threat_counts.data[i][0] = 0;
|
// threat_counts.data[i][0] = 0;
|
// threat_counts.data[i][1] = 0;
|
// threat_counts.data[i][1] = 0;
|
//}
|
//}
|
/*---------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------*/
|
// /* Horizontal lines */
|
// /* Horizontal lines */
|
// for (i = 0; i < board_size; i++)
|
// for (i = 0; i < board_size; i++)
|
// u_sum += threat_line(0, i, 1, 0,&b);
|
// u_sum += threat_line(0, i, 1, 0,&b);
|
|
|
// /* Vertical lines */
|
// /* Vertical lines */
|
// for (i = 0; i < board_size; i++)
|
// for (i = 0; i < board_size; i++)
|
// u_sum += threat_line(i, 0, 0, 1,&b);
|
// u_sum += threat_line(i, 0, 0, 1,&b);
|
|
|
// /* SE diagonals */
|
// /* SE diagonals */
|
// for (i = 0; i < board_size - connect_k + 1; i++)
|
// for (i = 0; i < board_size - connect_k + 1; i++)
|
// u_sum += threat_line(i, 0, 1, 1,&b);
|
// u_sum += threat_line(i, 0, 1, 1,&b);
|
// for (i = 1; i < board_size - connect_k + 1; i++)
|
// for (i = 1; i < board_size - connect_k + 1; i++)
|
// u_sum += threat_line(0, i, 1, 1,&b);
|
// u_sum += threat_line(0, i, 1, 1,&b);
|
|
|
// /* SW diagonals */
|
// /* SW diagonals */
|
// for (i = connect_k - 1; i < board_size; i++)
|
// for (i = connect_k - 1; i < board_size; i++)
|
// u_sum += threat_line(i, 0, -1, 1,&b);
|
// u_sum += threat_line(i, 0, -1, 1,&b);
|
// for (i = 1; i < board_size - connect_k + 1; i++)
|
// for (i = 1; i < board_size - connect_k + 1; i++)
|
// u_sum += threat_line(board_size - 1, i, -1, 1,&b);
|
// u_sum += threat_line(board_size - 1, i, -1, 1,&b);
|
/*---------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------*/
|
//rewritten for hardware
|
//rewritten for hardware
|
/*---------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------*/
|
//int id= PICO_initialize_PPA(threat_line);
|
//int id= PICO_initialize_PPA(threat_line);
|
//PICO_set_task_overlap(id, 2);
|
//PICO_set_task_overlap(id, 2);
|
int j;
|
int j;
|
int arg1,arg2,arg3,arg4,loop_bound,loop_begin;
|
int arg1,arg2,arg3,arg4,loop_bound,loop_begin;
|
int k=0;
|
int k=0;
|
for(j=0;j<6;j++){
|
for(j=0;j<6;j++){
|
switch(j){
|
switch(j){
|
case 0:
|
case 0:
|
{
|
{
|
loop_begin=0;
|
loop_begin=0;
|
loop_bound=board_size;
|
loop_bound=board_size;
|
break;
|
break;
|
}
|
}
|
case 1:
|
case 1:
|
{
|
{
|
loop_begin=0;
|
loop_begin=0;
|
loop_bound=board_size;
|
loop_bound=board_size;
|
break;
|
break;
|
}
|
}
|
case 2:
|
case 2:
|
{
|
{
|
loop_begin=0;
|
loop_begin=0;
|
loop_bound=board_size-connect_k+1;
|
loop_bound=board_size-connect_k+1;
|
break;
|
break;
|
}
|
}
|
case 3:
|
case 3:
|
{
|
{
|
loop_begin=1;
|
loop_begin=1;
|
loop_bound=board_size-connect_k+1;
|
loop_bound=board_size-connect_k+1;
|
break;
|
break;
|
}
|
}
|
case 4:
|
case 4:
|
{
|
{
|
loop_begin=connect_k-1;
|
loop_begin=connect_k-1;
|
loop_bound=board_size;
|
loop_bound=board_size;
|
break;
|
break;
|
}
|
}
|
case 5:
|
case 5:
|
{
|
{
|
loop_begin=1;
|
loop_begin=1;
|
loop_bound=board_size-connect_k+1;
|
loop_bound=board_size-connect_k+1;
|
break;
|
break;
|
}
|
}
|
default:{
|
default:{
|
break;
|
break;
|
}
|
}
|
}
|
}
|
for (i = loop_begin; i < loop_bound; i++){
|
for (i = loop_begin; i < loop_bound; i++){
|
k++;
|
k++;
|
switch(j){
|
switch(j){
|
case 0:
|
case 0:
|
{
|
{
|
arg1=0;
|
arg1=0;
|
arg2=i;
|
arg2=i;
|
arg3=1;
|
arg3=1;
|
arg4=0;
|
arg4=0;
|
break;
|
break;
|
}
|
}
|
case 1:
|
case 1:
|
{
|
{
|
arg1=i;
|
arg1=i;
|
arg2=0;
|
arg2=0;
|
arg3=0;
|
arg3=0;
|
arg4=1;
|
arg4=1;
|
break;
|
break;
|
}
|
}
|
case 2:
|
case 2:
|
{
|
{
|
arg1=i;
|
arg1=i;
|
arg2=0;
|
arg2=0;
|
arg3=1;
|
arg3=1;
|
arg4=1;
|
arg4=1;
|
break;
|
break;
|
}
|
}
|
case 3:
|
case 3:
|
{
|
{
|
arg1=0;
|
arg1=0;
|
arg2=i;
|
arg2=i;
|
arg3=1;
|
arg3=1;
|
arg4=1;
|
arg4=1;
|
break;
|
break;
|
}
|
}
|
case 4:
|
case 4:
|
{
|
{
|
arg1=i;
|
arg1=i;
|
arg2=0;
|
arg2=0;
|
arg3=-1;
|
arg3=-1;
|
arg4=1;
|
arg4=1;
|
break;
|
break;
|
}
|
}
|
case 5:
|
case 5:
|
{
|
{
|
arg1=board_size-1;
|
arg1=board_size-1;
|
arg2=i;
|
arg2=i;
|
arg3=-1;
|
arg3=-1;
|
arg4=1;
|
arg4=1;
|
break;
|
break;
|
}
|
}
|
default:{
|
default:{
|
break;
|
break;
|
}
|
}
|
}
|
}
|
|
|
|
|
if (k==1) board_copy(&b, &bwrite);
|
if (k==1) board_copy(&b, &bwrite);
|
u_sum += threat_line(arg1, arg2, arg3, arg4,&b,&bwrite,k,loop_bound);
|
u_sum += threat_line(arg1, arg2, arg3, arg4,&b,&bwrite,k,loop_bound);
|
}
|
}
|
}
|
}
|
//PICO_sync_task(id, 1);
|
//PICO_sync_task(id, 1);
|
//PICO_finalize_PPA(id);
|
//PICO_finalize_PPA(id);
|
/*---------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------*/
|
//board_copy(&b,&b_marks);
|
//board_copy(&b,&b_marks);
|
unsigned int index[max_size]={0};
|
//unsigned int index[max_size]={0};
|
#pragma bitsize index 9
|
#pragma bitsize index 9
|
#pragma internal_fast index
|
#pragma internal_fast index
|
AIMoves moves1;
|
AIMoves moves1;
|
|
moves1.len=0;
|
#pragma internal_blockram moves1
|
#pragma internal_blockram moves1
|
/*moves = */ ai_marks(&bwrite, PIECE_THREAT(1),&moves);
|
/*moves = */ ai_marks(&bwrite, PIECE_THREAT(1),&moves1);
|
//test(ready);
|
//test(ready);
|
streamsort(&moves1,&index[0]);
|
streamsort(moves,index);
|
//moves1.utility = u_sum;
|
moves1.utility = u_sum;
|
//moves.utility = u_sum;
|
moves->utility = u_sum;
|
/*----------------------------
|
/*----------------------------
|
rewritten for hardware
|
rewritten for hardware
|
----------------------------*/
|
----------------------------*/
|
//if (!aimoves_choose(&moves1, move))
|
//if (!aimoves_choose(&moves1, move))
|
// return 0;
|
// return 0;
|
//else return 1;
|
//else return 1;
|
int ret_val;
|
//int ret_val;
|
ret_val=aimoves_choose(&moves1, move,&index[0]);
|
//ret_val=aimoves_choose(&moves1, move,&index[0]);
|
if (!ret_val)
|
//if (!ret_val)
|
return 0;
|
// return 0;
|
else return 1;
|
//else return 1;
|
/*----------------------------
|
/*----------------------------
|
end rewritten for hardware
|
end rewritten for hardware
|
----------------------------*/
|
----------------------------*/
|
//board_free(b);
|
//board_free(b);
|
//return moves;
|
//return moves;
|
//return 0;
|
//return 0;
|
}
|
}
|
|
|
//void debug_counts(void)
|
//void debug_counts(void)
|
//{
|
//{
|
// int i, sum = 0;
|
// int i, sum = 0;
|
//
|
//
|
// if (!b)
|
// if (!b)
|
// return;
|
// return;
|
//
|
//
|
// g_debug("Threat counts (black, white):");
|
// g_debug("Threat counts (black, white):");
|
// for (i = 1; i < connect_k; i++) {
|
// for (i = 1; i < connect_k; i++) {
|
// g_debug("%d: %3d %3d", i, threat_counts[i][0],
|
// g_debug("%d: %3d %3d", i, threat_counts[i][0],
|
// threat_counts[i][1]);
|
// threat_counts[i][1]);
|
// sum += threat_counts[i][0] * threat_bits(i, b->turn) -
|
// sum += threat_counts[i][0] * threat_bits(i, b->turn) -
|
// threat_counts[i][1] *
|
// threat_counts[i][1] *
|
// threat_bits(i, other_player(b->turn));
|
// threat_bits(i, other_player(b->turn));
|
// }
|
// }
|
// if (sum > 0)
|
// if (sum > 0)
|
// g_debug("Threat sum: %d (10^%.2f)", sum, log10((double)sum));
|
// g_debug("Threat sum: %d (10^%.2f)", sum, log10((double)sum));
|
// else if (sum < 0)
|
// else if (sum < 0)
|
// g_debug("Threat sum: %d (-10^%.2f)", sum, log10((double)-sum));
|
// g_debug("Threat sum: %d (-10^%.2f)", sum, log10((double)-sum));
|
// else
|
// else
|
// g_debug("Threat sum: 0");
|
// g_debug("Threat sum: 0");
|
//}
|
//}
|
|
|
//static int threat_number(int player, int threat,threat_count_array threat_counts)
|
//static int threat_number(int player, int threat,threat_count_array threat_counts)
|
//{
|
//{
|
// return threat_counts.data[threat][player] / (connect_k - threat);
|
// return threat_counts.data[threat][player] / (connect_k - threat);
|
//}
|
//}
|
|
|
//AIMoves *ai_priority(const Board *b)
|
//AIMoves *ai_priority(const Board *b)
|
//{
|
//{
|
// AIMoves *moves;
|
// AIMoves *moves;
|
// int i, j, stage[2] = {1, 1}, mask, bits;
|
// int i, j, stage[2] = {1, 1}, mask, bits;
|
//
|
//
|
// moves = ai_threats(b);
|
// moves = ai_threats(b);
|
//
|
//
|
// /* Do not prioritize if we've won */
|
// /* Do not prioritize if we've won */
|
// if (threat_counts[connect_k - place_p + 1][b->turn - 1]) {
|
// if (threat_counts[connect_k - place_p + 1][b->turn - 1]) {
|
// moves->utility = AIW_WIN;
|
// moves->utility = AIW_WIN;
|
// return moves;
|
// return moves;
|
// }
|
// }
|
//
|
//
|
// /* Find the largest supported threat for each player */
|
// /* Find the largest supported threat for each player */
|
// for (i = 2; i < connect_k; i++) {
|
// for (i = 2; i < connect_k; i++) {
|
// if (threat_number(0, i - 1) >= place_p &&
|
// if (threat_number(0, i - 1) >= place_p &&
|
// threat_number(0, i) > place_p)
|
// threat_number(0, i) > place_p)
|
// stage[0] = i;
|
// stage[0] = i;
|
// if (threat_number(1, i - 1) >= place_p &&
|
// if (threat_number(1, i - 1) >= place_p &&
|
// threat_number(1, i) > place_p)
|
// threat_number(1, i) > place_p)
|
// stage[1] = i;
|
// stage[1] = i;
|
// }
|
// }
|
//
|
//
|
// //if (opt_debug_stage)
|
// //if (opt_debug_stage)
|
// // g_debug("Stages %d/%d", stage[0], stage[1]);
|
// // g_debug("Stages %d/%d", stage[0], stage[1]);
|
//
|
//
|
// /* Do not prioritize if we're losing */
|
// /* Do not prioritize if we're losing */
|
// if (stage[b->turn - 1] <= stage[other_player(b->turn) - 1]) {
|
// if (stage[b->turn - 1] <= stage[other_player(b->turn) - 1]) {
|
// moves->utility = -stage[other_player(b->turn) - 1];
|
// moves->utility = -stage[other_player(b->turn) - 1];
|
// return moves;
|
// return moves;
|
// }
|
// }
|
//
|
//
|
// /* Threats above the player's stage are no more valuable than the
|
// /* Threats above the player's stage are no more valuable than the
|
// stage */
|
// stage */
|
// bits = 1 << (stage[b->turn - 1] * BITS_PER_THREAT);
|
// bits = 1 << (stage[b->turn - 1] * BITS_PER_THREAT);
|
// mask = bits - 1;
|
// mask = bits - 1;
|
// for (i = 0; i < moves->len; i++) {
|
// for (i = 0; i < moves->len; i++) {
|
// AIWEIGHT w = moves->data[i].weight, w2;
|
// AIWEIGHT w = moves->data[i].weight, w2;
|
//
|
//
|
// if (w < AIW_THREAT_MAX && w >= bits) {
|
// if (w < AIW_THREAT_MAX && w >= bits) {
|
// w2 = w & mask;
|
// w2 = w & mask;
|
// w = w & ~mask;
|
// w = w & ~mask;
|
// for (j = stage[b->turn - 1];
|
// for (j = stage[b->turn - 1];
|
// w && j < connect_k - place_p + 1; j++) {
|
// w && j < connect_k - place_p + 1; j++) {
|
// w = w >> BITS_PER_THREAT;
|
// w = w >> BITS_PER_THREAT;
|
// w2 += w & mask;
|
// w2 += w & mask;
|
// }
|
// }
|
// moves->data[i].weight = w2;
|
// moves->data[i].weight = w2;
|
// }
|
// }
|
// }
|
// }
|
//
|
//
|
// /* Stage determines weight */
|
// /* Stage determines weight */
|
// moves->utility = stage[b->turn - 1];
|
// moves->utility = stage[b->turn - 1];
|
// return moves;
|
// return moves;
|
//}
|
//}
|
/*AIMoves*/ void ai_marks(Board *b, PIECE minimum,AIMoves *moves)
|
/*AIMoves*/ void ai_marks(Board *b, PIECE minimum,AIMoves *moves)
|
{
|
{
|
//#pragma read_write_ports b.data combined 2
|
//#pragma read_write_ports b.data combined 2
|
#pragma internal_blockram b
|
#pragma internal_blockram b
|
//#pragma no_memory_analysis b
|
//#pragma no_memory_analysis b
|
//AIMoves *moves = aimoves_new();
|
//AIMoves *moves = aimoves_new();
|
//AIMoves moves;
|
//AIMoves moves;
|
//AIMoves moves[361];
|
//AIMoves moves[361];
|
AIMove move;
|
AIMove move;
|
PIECE p;
|
PIECE p;
|
//moves_fifo.resoet();
|
//moves_fifo.resoet();
|
AIMove m;
|
AIMove m;
|
#pragma num_iterations(19,19,19)
|
#pragma num_iterations(19,19,19)
|
for (move.y = 0; move.y < board_size; move.y++)
|
for (move.y = 0; move.y < board_size; move.y++)
|
#pragma num_iterations(19,19,19)
|
#pragma num_iterations(19,19,19)
|
for (move.x = 0; move.x < board_size; move.x++){
|
for (move.x = 0; move.x < board_size; move.x++){
|
if ((p = piece_at(b, move.x, move.y)) >= minimum) {
|
if ((p = piece_at(b, move.x, move.y)) >= minimum) {
|
move.weight = p - PIECE_THREAT0;
|
move.weight = p - PIECE_THREAT0;
|
aimoves_set(moves, &move);
|
aimoves_set(moves, &move);
|
pico_stream_output_queue(move);
|
pico_stream_output_queue(move);
|
//cout<<"push"<<move.weight<<endl;
|
//cout<<"push"<<move.weight<<endl;
|
//ready=ready+1;
|
//ready=ready+1;
|
//cout<<"READY"<<*ready<<endl;
|
//cout<<"READY"<<*ready<<endl;
|
}else {
|
}else {
|
m.weight =-100;
|
m.weight =-100;
|
pico_stream_output_queue(m);
|
pico_stream_output_queue(m);
|
//cout<<"push"<<m.weight<<endl;
|
//cout<<"push"<<m.weight<<endl;
|
}
|
}
|
//if((move.y == board_size-1) && (move.x == board_size-1)){
|
//if((move.y == board_size-1) && (move.x == board_size-1)){
|
// m.weight=-1;m.x=-1;m.y=-1;
|
// m.weight=-1;m.x=-1;m.y=-1;
|
// /*if((ready))*/ {pico_stream_output_queue(m);/*cout<<"push_finish"<<m.weight<<endl;*/}
|
// /*if((ready))*/ {pico_stream_output_queue(m);/*cout<<"push_finish"<<m.weight<<endl;*/}
|
//
|
//
|
//}
|
//}
|
}
|
}
|
//moves_fifo.active=0;
|
//moves_fifo.active=0;
|
//return moves;
|
//return moves;
|
int i;
|
int i;
|
//for (i=0;i<10;i++) {*ready=i;pico_stream_output_x(i); }
|
//for (i=0;i<10;i++) {*ready=i;pico_stream_output_x(i); }
|
//for (i=0;i<10;i++) {if(!moves_fifo1.full()) moves_fifo1.push(m); }
|
//for (i=0;i<10;i++) {if(!moves_fifo1.full()) moves_fifo1.push(m); }
|
//for (i=0;i<10;i++) {
|
//for (i=0;i<10;i++) {
|
// *ready=i;pico_stream_output_queue(m);
|
// *ready=i;pico_stream_output_queue(m);
|
// if(i==9){
|
// if(i==9){
|
// m.weight=-1;
|
// m.weight=-1;
|
// pico_stream_output_queue(m);
|
// pico_stream_output_queue(m);
|
// }
|
// }
|
//
|
//
|
// }
|
// }
|
|
|
}
|
}
|
void streamsort(AIMoves *moves,unsigned int *index){
|
void streamsort(AIMoves *moves,unsigned int *index){
|
/* Insertion sort for streaming*/
|
/* Insertion sort for streaming*/
|
AIMove val;
|
AIMove val;
|
AIMove data[361]={{-1},{-1},{-1}};
|
AIMove data[361]={{-1},{-1},{-1}};
|
//unsigned int index[361];
|
//unsigned int index[361];
|
#pragma bitsize index 9
|
#pragma bitsize index 9
|
#pragma internal_fast index
|
#pragma internal_fast index
|
int i,j,k;
|
int i,j,k;
|
unsigned int len=0;
|
unsigned int len=0;
|
moves->data[0].weight=-100;
|
moves->data[0].weight=-100;
|
// while(1) {
|
// while(1) {
|
// if(!moves_fifo.empty()){
|
// if(!moves_fifo.empty()){
|
// val=moves_fifo.pop();
|
// val=moves_fifo.pop();
|
// for(i=0;i<len;i++){
|
// for(i=0;i<len;i++){
|
// if (list[i].weight < val.weight){
|
// if (list[i].weight < val.weight){
|
// for(j=len-1;j>i-1;j--){
|
// for(j=len-1;j>i-1;j--){
|
// list[j+1]=list[j];
|
// list[j+1]=list[j];
|
// }
|
// }
|
// break;
|
// break;
|
// }
|
// }
|
// }
|
// }
|
// list[i]=val;
|
// list[i]=val;
|
// len++;
|
// len++;
|
// }
|
// }
|
// else break;
|
// else break;
|
// //if(!moves_fifo.active && moves_fifo.empty()) break;
|
// //if(!moves_fifo.active && moves_fifo.empty()) break;
|
// }
|
// }
|
//while(1) {
|
//while(1) {
|
//int count=0;
|
//int count=0;
|
#pragma num_iterations(1,150,368)
|
#pragma num_iterations(1,150,368)
|
for(k=0;k<368;k++){
|
for(k=0;k<368;k++){
|
//count++;
|
//count++;
|
//cout<<count<<endl;
|
//cout<<count<<endl;
|
if (k>6){
|
if (k>6){
|
|
|
//if(ready>5){
|
//if(ready>5){
|
val=pico_stream_input_queue();
|
val=pico_stream_input_queue();
|
//cout<<"popped"<<","<<val.weight<<" "<<val.x<<" "<<val.y<<endl;
|
//cout<<"popped"<<","<<val.weight<<" "<<val.x<<" "<<val.y<<endl;
|
if(val.weight==-1) {moves->len=len;break;}
|
if(val.weight==-1) {moves->len=len;break;}
|
else if(val.weight==-100) continue;
|
else if(val.weight==-100) continue;
|
#pragma num_iterations(0,150,361)
|
#pragma num_iterations(0,150,361)
|
for(i=0;i<len;i++){
|
for(i=0;i<len;i++){
|
if (moves->data[index[i]].weight < val.weight){
|
if (moves->data[index[i]].weight < val.weight){
|
for(j=len-1;j>i-1;j--){
|
for(j=len-1;j>i-1;j--){
|
//moves->data[j+1]=moves->data[j];
|
//moves->data[j+1]=moves->data[j];
|
index[j+1]=index[j];
|
index[j+1]=index[j];
|
}
|
}
|
break;
|
break;
|
}
|
}
|
}
|
}
|
index[i]=len;
|
index[i]=len;
|
moves->data[len]=val;
|
moves->data[len]=val;
|
len++;
|
len++;
|
//cout<<"STREAMSORT"<<":";
|
//cout<<"STREAMSORT"<<":";
|
//}
|
//}
|
/*else*/ //{moves->len=len;break;}
|
/*else*/ //{moves->len=len;break;}
|
}
|
}
|
}
|
}
|
moves->len=len;
|
moves->len=len;
|
//cout<<"STREAMSORT"<<":"<<moves->len<<endl;
|
//cout<<"STREAMSORT"<<":"<<moves->len<<endl;
|
//for(i=0;i<len;i++) cout<<moves->data[i].weight<<",";
|
//for(i=0;i<len;i++) cout<<moves->data[i].weight<<",";
|
//cout<<endl;
|
//cout<<endl;
|
//for(i=0;i<len;i++) cout<<moves->data[index[i]].weight<<",";
|
//for(i=0;i<len;i++) cout<<moves->data[index[i]].weight<<",";
|
//cout<<endl;
|
//cout<<endl;
|
}
|
}
|
static gboolean is_adjacent( Board *b, BCOORD x, BCOORD y, int dist)
|
static gboolean is_adjacent( Board *b, BCOORD x, BCOORD y, int dist)
|
{
|
{
|
int dx, dy, count;
|
int dx, dy, count;
|
#pragma bitsize dx 4
|
#pragma bitsize dx 4
|
#pragma bitsize dy 4
|
#pragma bitsize dy 4
|
PIECE p;
|
PIECE p;
|
|
|
if (!piece_empty(piece_at(b, x, y)))
|
if (!piece_empty(piece_at(b, x, y)))
|
return FALSE;
|
return FALSE;
|
for (dy = -1; dy < 2; dy++)
|
for (dy = -1; dy < 2; dy++)
|
for (dx = -1; dx < 2; dx++) {
|
for (dx = -1; dx < 2; dx++) {
|
if (!dx && !dy)
|
if (!dx && !dy)
|
continue;
|
continue;
|
count = count_pieces(b, x, y, PIECE_NONE, dx, dy, &p);
|
count = count_pieces(b, x, y, PIECE_NONE, dx, dy, &p);
|
if (count - 1 < dist && p != PIECE_NONE)
|
if (count - 1 < dist && p != PIECE_NONE)
|
return TRUE;
|
return TRUE;
|
}
|
}
|
return FALSE;
|
return FALSE;
|
}
|
}
|
/*AIMoves **/ void enum_adjacent(Board *b, int dist,AIMoves *moves,unsigned int current_random)
|
/*AIMoves **/ void enum_adjacent(Board *b, int dist,AIMoves *moves,unsigned int current_random)
|
{
|
{
|
//AIMoves *moves;
|
//AIMoves *moves;
|
AIMove move;
|
AIMove move;
|
|
|
move.weight = AIW_NONE;
|
move.weight = AIW_NONE;
|
//moves = aimoves_new();
|
//moves = aimoves_new();
|
#pragma num_iterations(1,9,19)
|
#pragma num_iterations(1,9,19)
|
for (move.y = 0; move.y < board_size; move.y++){
|
for (move.y = 0; move.y < board_size; move.y++){
|
#pragma num_iterations(1,9,19)
|
#pragma num_iterations(1,9,19)
|
for (move.x = 0; move.x < board_size; move.x++){
|
for (move.x = 0; move.x < board_size; move.x++){
|
if (is_adjacent(b, move.x, move.y, dist))
|
if (is_adjacent(b, move.x, move.y, dist))
|
aimoves_append(moves, &move);
|
aimoves_append(moves, &move);
|
}
|
}
|
}
|
}
|
//aimoves_shuffle(moves,current_random);
|
//aimoves_shuffle(moves,current_random);
|
//return moves;
|
//return moves;
|
}
|
}
|
/*AIMoves **/void ai_adjacent( Board *b, AIMove *move,unsigned int current_random)
|
/*AIMoves **/void ai_adjacent( Board *b, AIMove *move,unsigned int current_random)
|
{
|
{
|
//#pragma read_write_ports board.data combined 2
|
//#pragma read_write_ports board.data combined 2
|
#pragma internal_blockram b
|
#pragma internal_blockram b
|
//#pragma no_memory_analysis b
|
//#pragma no_memory_analysis b
|
|
|
/*static*/ AIMoves moves;//={{0,0,0,{{0,0,0}}}};
|
/*static*/ AIMoves moves;//={{0,0,0,{{0,0,0}}}};
|
//#pragma read_write_ports moves.data combined 3
|
//#pragma read_write_ports moves.data combined 3
|
#pragma internal_blockram moves
|
#pragma internal_blockram moves
|
//#pragma no_memory_analysis moves
|
//#pragma no_memory_analysis moves
|
//#pragma read_write_ports moves.data combined 3
|
//#pragma read_write_ports moves.data combined 3
|
//#pragma internal_blockram moves
|
//#pragma internal_blockram moves
|
//#pragma no_memory_analysis moves
|
//#pragma no_memory_analysis moves
|
//AIMove move;
|
//AIMove move;
|
unsigned int index[1]={0};
|
unsigned int index[1]={0};
|
//AIMoves *moves;
|
//AIMoves *moves;
|
moves.len=0;
|
moves.len=0;
|
/* Get all open tiles adjacent to any piece */
|
/* Get all open tiles adjacent to any piece */
|
/*moves =*/ enum_adjacent(b, 1,&moves,current_random);
|
/*moves =*/ enum_adjacent(b, 1,&moves,current_random);
|
if (moves.len){
|
if (moves.len){
|
aimoves_choose(&moves, move,&index[0]);
|
aimoves_choose(&moves, move,&index[0]);
|
|
|
return ;//moves;
|
return ;//moves;
|
}
|
}
|
/* Play in the middle if there are no open adjacent tiles */
|
/* Play in the middle if there are no open adjacent tiles */
|
move->x = board_size / 2;
|
move->x = board_size / 2;
|
move->y = board_size / 2;
|
move->y = board_size / 2;
|
move->weight = AIW_NONE;
|
move->weight = AIW_NONE;
|
//aimoves_append(&moves, move);
|
//aimoves_append(&moves, move);
|
//aimoves_choose(&moves, move);
|
//aimoves_choose(&moves, move);
|
//return moves;
|
//return moves;
|
}
|
}
|
|
|