#include "BaseBallClient.h"
WSADATA wsdata // 전역변수로소켓변수들을선언
SOCKET clientSocket
struct sockaddr_in serverAddress
int ConnectServer(){
if(WSAStartup(MAKEWORD(2,2), &wsdata) != 0){ // 소켓초기화
printf("Fail initiallize socket\n");
return 0;
}
printf("Initiallize socket\n");
clientSocket = socket(AF_INET, SOCK_STREAM, 0);
// 소켓연결을생성
if(clientSocket == INVALID_SOCKET){
printf("Fail make socket\n");
return 0;
}
printf("Make client socket\n"); // 소켓옵션을주고서버에연결
ZeroMemory(&serverAddress, sizeof(serverAddress));
serverAddress.sin_addr.s_addr = inet_addr("127.0.0.1");
serverAddress.sin_family = AF_INET
serverAddress.sin_port = htons(PORT);
connect(clientSocket, (struct sockaddr *)&serverAddress, sizeof(serverAddress));
printf("Play~~~~~Ball~~~!!!\n");
}
void playBaseBall(){ // 야구게임
int clientNum[3]; // 입력숫자
int score[3]; // 볼카운트
int i // 반복문인덱스
int strike, ball, out // SBO
while(1){
printf("Now client turn\n"); // 클라이언트턴
do{
printf("Input Num : "); // 입력을받도록한다
scanf_s("%d %d %d", &clientNum[0], &clientNum[1], &clientNum[2]);
}// 같은숫자입력이나0-9범위밖의입력예외처리
while(clientNum[0] == clientNum[1] || clientNum[0] == clientNum[2] || clientNum[1] == clientNum[2] ||
clientNum[0] < 0 || clientNum[0] > 9 || clientNum[1] < 0 || clientNum[1] > 9 || clientNum[2] < 0 || clientNum[2] > 9);
for(i = 0; i<3; i++){
clientNum[i] = htonl(clientNum[i]);
}
// 입력한숫자를서버로저농
send(clientSocket, (char*)clientNum, sizeof(clientNum), 0);
// 입력에대한볼카운트를서버로부터수신
recv(clientSocket, (char*)score, sizeof(score), 0);
strike = ntohl(score[0]);
ball = ntohl(score[1]);
out = ntohl(score[2]);
// 결과를출력
printf("[Client] -> S : %d B : %d O : %d\n", strike, ball, out);
if(strike == 3){ // 3 스트라이크면클라리언트승리/ 종료
printf("Client win!!\n");
printf("End~~Ball~~!!\n");
closesocket(clientSocket);
return ;
}
printf("Now Server turn\n"); // 서버턴
// 서버에서입력된숫자를수신
recv(clientSocket, (char*)clientNum, sizeof(clientNum), 0);
for(i=0; i < 3; i++){
clientNum[i] = ntohl(clientNum[i]);
}
// 서버에서입력한숫자를출력
printf("Server Input : %d %d %d\n", clientNum[0], clientNum[1], clientNum[2]);
// 볼카운트결과를수신
recv(clientSocket, (char*)score, sizeof(score), 0);
strike = ntohl(score[0]);
ball = ntohl(score[1]);
out = ntohl(score[2]);
// 결과를출력
printf("[Server] -> S : %d B : %d O : %d\n", strike, ball, out);
if(strike == 3){ // 3스트라이크면서버의승리
printf("server win~!!\n");
printf("End~~Ball~~!!\n");
//closesocket(clientSocket);
return ;
}
}
}