トップ 差分 一覧 ソース 検索 ヘルプ PDF RSS ログイン

3moku

三目並べ

  作業環境

  • Cygwinのホームディレクトリ以下に
ホーム/soc/

あるいは

ホーム/soc/3moku/

のようなディレクトリを作成し、そこで作業を行う。

  ソース

goban.h

/*********************************************/
/*       File name:  goban.h                 */
/*    Project name:  socket/3moku            */
/*      Month/Year:  Jan/2003                */
/*          Author:  Shuichi YUKITA          */
/*********************************************/

#include <sys/types.h>
#include "mylib.h"

/* constants definitions */
#define PORT	(u_short)50000

/* public functions */
extern int goban_init(int soc, char my_stone, char peer_stone);
extern void goban_show_plane();
extern int goban_peer_turn();
extern int goban_my_turn();

goban.c

/*********************************************/
/*       File name:  goban.c                 */
/*    Project name:  socket/3moku            */
/*      Month/Year:  Jan/2003                */
/*          Author:  Shuichi YUKITA          */
/*********************************************/

#include "goban.h"

char goban_my_stone;
char goban_peer_stone;
char goban_plane[5][6] = { "+123+",
			"a...|","b...|","c...|","+---+"};
int goban_soc;

int goban_init(int soc, char my_stone, char peer_stone)
{
  goban_soc=soc;
  goban_my_stone=my_stone;
  goban_peer_stone=peer_stone;
}

void goban_show()
{
  int i;
  for(i=0;i<5;i++) {
    printf("%s\n",goban_plane[i]);
  }
}

int goban_peer_turn()
{
  char	data[10];
  int	x,y;

  read(goban_soc,data,10);
  if ( data[0] == 'q' ) return -1;
  y = (int)data[0] - (int)'a' + 1;
  x = (int)data[1] - (int)'0';
  goban_plane[y][x] = goban_peer_stone;
  return 1;
}

int goban_my_turn()
{
  int	x,y;
  char	data[10];

  while(1){
    fgets(data, 10, stdin);
    if( data[0] == 'q' )
      { write(goban_soc,data,1); return	-1; }
    if( data[0] < 'a' || data[0] > 'c' ||
	data[1] < '1' ||  data[1] > '3' ) continue;
    break;
  }

  y = data[0]-'a'+1; x = data[1]-'0';
  goban_plane[y][x] = goban_my_stone;
  write(goban_soc,data,10);
  return 1;
}

void goban_destroy(){
  close(goban_soc);
}

server.c

/*********************************************/
/*       File name:  server.c                */
/*    Project name:  socket/3moku            */
/*      Month/Year:  Jan/2003                */
/*          Author:  Shuichi YUKITA          */
/*********************************************/

#include "goban.h"

main()
{
  int soc;
  char my_stone='o';
  char peer_stone='x';

  if( (soc=setup_server(PORT))==-1){
    exit(1);
  }

  if(  goban_init(soc, my_stone, peer_stone)==-1){
    exit(1);
  }

  while(1){
    goban_show(); printf("Wait.\n");
    if( goban_peer_turn() == -1 ) break;
    goban_show(); printf("Go ahead.\n");
    if( goban_my_turn() == -1 ) break;
  }

  goban_destroy();
}

client.c

/*********************************************/
/*       File name:  client.c                */
/*    Project name:  socket/3moku            */
/*      Month/Year:  Jan/2003                */
/*          Author:  Shuichi YUKITA          */
/*********************************************/

#include "goban.h"

#define HOSTNAME_LENGTH 64

main()
{
  int soc;
  char my_stone='x';
  char peer_stone='o';
  char hostname[64];

  printf("Input  server's  hostname: ");
  fgets(hostname, HOSTNAME_LENGTH, stdin);
  chop_newline(hostname, HOSTNAME_LENGTH);

  if( (soc=setup_client(hostname, PORT))==-1){
    exit(1);
  }

  if(  goban_init(soc,my_stone,peer_stone)==-1){
    exit(1);
  }

  while(1){
    goban_show(); printf("Go ahead.\n");
    if( goban_my_turn() == -1 ) break;
    goban_show(); printf("Wait.\n");
    if( goban_peer_turn() == -1 ) break;
  }

  goban_destroy();
}

  ビルド Makefile

#*********************************************/
#*       File name:  Makefile                */
#*    Project name:  socket/3moku            */
#*      Month/Year:  Jan/2003                */
#*          Author:  Shuichi YUKITA          */
#*********************************************/
#
#  "root.o" target is implicitly built by
#  $(COMPILE.c) -o root.o root.c

MYLIBDIR=../mylib
CFLAGS=-I$(MYLIBDIR)
RM=rm -f

all:	s c

s:	server.o  goban.o
	$(LINK.c) -o s server.o goban.o ${MYLIBDIR}/mylib.a

c:	client.o  goban.o
	$(LINK.c) -o c client.o goban.o ${MYLIBDIR}/mylib.a

server.o:	server.c goban.h

client.o:	client.c goban.h

clean:
	$(RM) s c *.o *~

  実行

初めの一歩 と同じ。