2012. 2. 11. 16:37
//2012 02 10
//연산자오버로딩총집합!! Operate Overloading!!
//i_co
//Daun..

//영어따위 틀려도... 나만 알아 듣는다면... 휴우 ㅠㅠ

#include<iostream>
#include<stdlib.h>
#include<time.h>
using namespace std;


class Complex 
{
public:
Complex();
Complex(int,int);
friend ostream &operator<<(ostream &,Complex &);
friend istream &operator>>(istream &,Complex &);

Complex operator++();
Complex operator--();
Complex operator++(int);
Complex operator--(int);
Complex operator+(Complex &);
Complex operator-(Complex &);
Complex operator*(Complex &);
Complex operator/(Complex &);

Complex operator+=(Complex);
Complex operator-=(Complex);
Complex operator*=(Complex);
Complex operator/=(Complex);

Complex operator=(Complex);

bool operator==(Complex);
bool operator!=(Complex);
bool operator<(Complex);
bool operator>(Complex);
bool operator<=(Complex);
bool operator>=(Complex);

Complex operator[](int);

void operator()();



private:
int realNum, imaginaryNum;
};

/*****  main ******/

int main()
{
srand(time(NULL));

Complex com1(3,4);
Complex com2;
cout << "Write com2 : ";
cin >> com2;
cout << "com1 : " << com1 << endl;
cout << "com2 : " << com2 << endl;
cout << "(++com1) + com2 " << (++com1) + com2 << endl;
cout << "(com1++) + com2 " << (com1++) + com2 << endl;
cout << "com1 + com2 " << com1 + com2 << endl;
cout << "com1 - com2 " << com1 - com2 << endl;
cout << "com1 * com2 " << com1 * com2 << endl;
cout << "com1 / com2 " << com1 / com2 << endl;
cout << "com1 += com2 "<< endl;
com1 += com2;
cout << "com1 : " << com1 << " com2 : " << com2 << endl;
cout << "com1 -= com2 " << endl;
com1 -= com2;
cout << "com1 : " << com1 << " com2 : " << com2 << endl;
cout << "com1 *= com2 " << endl;
com1 *= com2;
cout << "com1 : " << com1 << " com2 : " << com2 << endl;
cout << "com1 /= com2 " << endl;
com1 /= com2;
cout << "com1 : " << com1 << " com2 : " << com2 << endl;

if(com1 == com2)
cout << "com1 == com2" << endl;
else
cout << "com1 != com2" << endl;

cout << "com1() " << endl;
com1();
cout << "com1 : " << com1 << endl;

cout << "com1[3] " << endl;
com1[3];
cout << "com1 : " << com1 << endl;

return 0;
}


/************ Class Complex *******/


/* Defualt Constructor*/
Complex::Complex()
{
realNum = 0;
imaginaryNum = 0;
}

/* Constructor */
Complex::Complex(int REALNUM, int IMAGINARYNUM)
{
realNum = REALNUM;
imaginaryNum = IMAGINARYNUM;
}




/* Operator Overloading << */
ostream &operator<<(ostream &c,Complex &com)
{
c << com.realNum << ' ' << com.imaginaryNum << 'i';
return c;
}
/* Operator Overloading >>*/
istream &operator>>(istream &c,Complex &com)
{
c >> com.realNum >> com.imaginaryNum;
return c;
}





/* Operator Overloading ++ */
Complex Complex::operator++()
{
this->realNum++;
return *this;
}


/* Operator Overloading -- */
Complex Complex::operator--()
{
this->realNum--;
return *this;
}

/* Operator Overloading ++ */
Complex Complex::operator++(int)
{
int temp = this->realNum;
this->realNum++;
return Complex(temp,this->imaginaryNum);
}

/* Operator Overloading -- */
Complex Complex::operator--(int)
{
int temp = this->realNum;
this->realNum--;
return Complex(temp, this->imaginaryNum);
}






/* Operator Overloading + */
Complex Complex::operator+(Complex &com)
{
Complex temp;
temp.realNum = realNum + com.realNum;
temp.imaginaryNum = imaginaryNum + com.imaginaryNum;
return temp;
}

/* Operator Overloading - */
Complex Complex::operator-(Complex &com)
{
Complex temp;
temp.realNum = realNum - com.realNum;
temp.imaginaryNum = imaginaryNum - com.imaginaryNum;
return temp;
}

/* Operator Overloading * */
Complex Complex::operator *(Complex &com)
{
Complex temp;
temp.realNum = realNum * com.realNum;
temp.imaginaryNum = imaginaryNum * com.imaginaryNum;
return temp;
}

/* Operator Overloading / */
Complex Complex::operator/(Complex &com)
{
Complex temp;
temp.realNum = realNum / com.realNum;
temp.imaginaryNum = imaginaryNum / com.imaginaryNum;
return temp;
}




/* Operator Overloading += */
Complex Complex::operator+=(Complex com)
{
this->realNum += com.realNum;
this->imaginaryNum += com.imaginaryNum;
return *this;
}

/* Operator Overloading -= */
Complex Complex::operator-=(Complex com)
{
this->realNum -= com.realNum;
this->imaginaryNum -= com.imaginaryNum;
return *this;
}

/* Operator Overloading *= */
Complex Complex::operator*=(Complex com)
{
this->realNum *= com.realNum;
this->imaginaryNum *= com.imaginaryNum;
return *this;
}

/* Operator Overloading /= */
Complex Complex::operator/=(Complex com)
{
this->realNum /= com.realNum;
this->imaginaryNum /= com.imaginaryNum;
return *this;
}


/* Operator Overloading = */
Complex Complex::operator=(Complex com)
{
this->realNum = com.realNum;
this->imaginaryNum = com.imaginaryNum;
return *this;
}



/* Operator Overloading == */
bool Complex::operator==(Complex com)
{
if((this->realNum == com.realNum) && (this->imaginaryNum == com.imaginaryNum))
return 1;
else
return 0;
}

/* Operator Overloading != */
bool Complex::operator!=(Complex com)
{
if((this->realNum != com.realNum) && (this->imaginaryNum != com.imaginaryNum))
return 1;
else 
return 0;
}

/* Operator Overloading < */
bool Complex::operator<(Complex com)
{
if(this->realNum < com.realNum)
{
if(this->imaginaryNum < com.imaginaryNum)
return 1;
else
return 0;
}
return 0;
}

/* Operator Overloading > */
bool Complex::operator>(Complex com)
{
if(this->realNum > com.realNum)
{
if(this->imaginaryNum > com.imaginaryNum)
return 1;
else
return 0;
}
return 0;
}

/* Operator Overloading <= */
bool Complex::operator<=(Complex com)
{
if(this->realNum <= com.realNum)
{
if(this->imaginaryNum <= com.imaginaryNum)
return 1;
else
return 0;
}
return 0;

}

/* Operator Overloading >= */
bool Complex::operator>=(Complex com)
{
if(this->realNum >= com.realNum)
{
if(this->imaginaryNum >= com.imaginaryNum)
return 1;
else
return 0;
}
return 0;
}


/*
[] make that realNum and imaginaryNum are changed together. And plus indx to realNum and imaginaryNum.
() make that realNum and imaginaryNum have new number randomly.
*/


/* Operator Overloading [] */
Complex Complex::operator[](int indx)
{
cout << "CHANGE!! " << endl;
int temp = realNum;
realNum = imaginaryNum;
imaginaryNum = temp;

realNum += indx;
imaginaryNum += indx;

return *this;
}

/* Operator Overloading () */
void Complex::operator()()
{
cout << "RANDOM!! " << endl;
this->realNum = (rand() % 9 + 1);
this->imaginaryNum = (rand() % 9 + 1 );
}

'Code > c/c++' 카테고리의 다른 글

[C++]Double Linked List 기본기능만  (0) 2011.12.23
[C++]Game of Snake.  (0) 2011.09.03
[C++]poker  (0) 2011.09.03
[Code] 유클리드 알고리즘(Euclid Algorism)  (0) 2011.08.29
[C++ Code]Swap  (0) 2011.07.28
Posted by I_co
2012. 1. 21. 00:32
1.쿠키 설정 함수

쿠키설정 함수는 브라우저에 어떠한 출력이 되기전에 호출해야 합니다.

(http 헤더에 포함되기 때문)

 

1.1 setcookie()

bool setcookie(string name [, string value [, int expire [, string path [,string domain[, bool secure]]]]])

$id=testId

$lifetime = 60*60*24*30; 

// 60초(1분)*60 = 1시간

// 60*60*24= 하루

// 60*60*24*30 = 30일


http://icoi.tistory.com/script/powerEditor/pages/

$ckName='.test.co.kr'

 예) setcookie( "user_id" , $id , $lifetime  , '/',  $ckName);


1.2 setrawcookie()

bool setrawcookie ( string name [, string value [, int expire [, string path [, string domain [, int secure]]]]])

- php5에서 사용가능

- setcookie()와 거의 동일하나 urlencoded가 자동적으로 이루어 되지 않는다

 

인자

의미

name

쿠키 이름

value

클라이언트에 저장할 데이터

expire

쿠키만료시간(GMT 사용)

지정하지 않으면 브로우저가 닫힐 때 만료

time(),mktime() 함수를 사용하여 시간 설정

path

쿠키의 유효 경로

통상 /(루트)로 설정한다.

domain

쿠키의 유효 도메인

secure

0:비보안 1:보안 연결

 

1.3 return 

정상적으로 전송되었으면 TRUE 반환

아니면 FALSE 리턴

 

2.클라이언트에서 전송된 쿠키 참조

$_COOKIE[name];  è 자동전역배열

$_REQUEST

 

클라이언트로부터 전송된 쿠키 데이터는 register_globals variables_order 환경 설정 변수에 의존하여 자동적으로 PHP 변수로 변환됩니다.

하나의 쿠키 이름에 여러 변수를 지정하고 싶을 때는단순히 쿠키 이름에 []를 추가하면 됩니다.

 

3.예제

 

3.1 쿠키의 설정

<?php

$value = 'lynx';

 

//expire 지정하지 않으면, 쿠키는 세션 종료시(브라우저를 닫을 때)에 만료  

setcookie("user", $value); 

//setcookie("user", $value,time()+3600);  /* 1시간 뒤에 만료 */

//setcookie("user", $value,time()+3600, "/", ".example.com", 1);

?>

 

3.2 쿠키값 읽기

<?php

echo $_COOKIE["user"];

print_r($_COOKIE);  //쿠키에 설정된 값 write

?>

 

 

 

 

 

<?php

// 쿠키를 설정

setcookie("cookie[three]", "cookiethree");

setcookie("cookie[two]", "cookietwo");

setcookie("cookie[one]", "cookieone");

?>

 

 

 

 

 

 

 

3.3 쿠키와 배열

 

<?php

// 쿠키를 읽기

// 페이지가 리로드된 뒤에출력

if (isset($_COOKIE['cookie'])) {

    foreach ($_COOKIE['cookie'] as $name => $value) {

        echo "$name : $value <br />\n";

    }

}

?>

 

 

 

 

 

 

 

 

 

 

출력은

three : cookiethree

two : cookietwo

one : cookieone

 

 

 

 

 

3.4 쿠키 삭제

<?php

// 만료 날짜를 한시간전으로 설정

setcookie ("TestCookie", "", time() - 3600);

?>

 

 

 

 

 

'Code > web' 카테고리의 다른 글

[html+php+mysql]홈페이지 만들기.  (10) 2011.09.03
Posted by I_co
2011. 12. 23. 00:52
//DoubleLinkedList.cpp
//2011 12 22
//Daun 
//넣고 출력하는기능밖에 없음 
#include<iostream>
using namespace std;

class Node
{
public:
Node()
{
beforeNode=0;
afterNode=0;
num = 0;
}
int num;
Node *beforeNode;
Node *afterNode;
};
class Manage
{
public:
Manage()
{
firstNode = new Node;
newNode = new Node;
newNode->beforeNode = firstNode;
firstNode->afterNode = newNode;
}
void make(int temp)
{
Node *nowNode = firstNode;
nowNode = nowNode->afterNode;

while(nowNode->num != 0 )
nowNode = nowNode->afterNode;

Node *newNode = new Node;
nowNode->num = temp;
nowNode->afterNode = newNode;
newNode->beforeNode = nowNode;
}
void printFirst()
{
Node *nowNode = firstNode;
nowNode = nowNode->afterNode;
while(nowNode->num != 0 )
{
cout << nowNode->num << endl;
nowNode = nowNode->afterNode;
}
}
void printLast()
{
Node *nowNode = firstNode;
nowNode = nowNode->afterNode;
while(nowNode->num != 0 )
nowNode = nowNode->afterNode;

nowNode = nowNode->beforeNode;

while(nowNode->num !=0)
{
cout << nowNode->num << endl;
nowNode = nowNode->beforeNode;
}
}
private:
Node *firstNode;
Node *newNode;
};
int main()
{
int type;
Manage ma;
while(1)
{
type = 0;
cout << "press the type" << endl;
cout << "1 : insert, 2 : print from first , 3 : print from last" << endl;
cin >> type;
switch(type)
{
case 1:
int temp;
cout << "write the number without '0'"<<endl;;
cin >> temp;
ma.make(temp);
break;
case 2:
ma.printFirst();
break;
case 3:
ma.printLast();
break;
}
}
return 0;
}

'Code > c/c++' 카테고리의 다른 글

[C++]연산자오버로딩  (0) 2012.02.11
[C++]Game of Snake.  (0) 2011.09.03
[C++]poker  (0) 2011.09.03
[Code] 유클리드 알고리즘(Euclid Algorism)  (0) 2011.08.29
[C++ Code]Swap  (0) 2011.07.28
Posted by I_co
2011. 9. 3. 14:55


//2011.06.28
//daun

#include <iostream>
#include <conio.h>
#include <windows.h>
#include <time.h>
#define ON 1
#define OFF 0
using namespace std;

//전체 크기 80*25
//0번은 배경 1번은 뱀의몸통 2번은 먹이
//3이 되면 count가 1이 증가하고 ... 이런식으로해야겟군.. 아아아아아아아아 복잡하다 일단은 해봅시다

void headWay(int, int *, int *);

struct room{
 int time;
 int type;
};


int main()
{
 srand(time(NULL));
 int count=1 ; //먹이를 먹은 횟수이자 뱀의 몸통길이를 나타냄
 room arr[70][23]={0,0};
 int headx=0, heady=0;
 int type=117;
 int foodx =0, foody = 0;
 int level;
 int leveltime = 0;
 cout << "1~10 Level중 몇 Level을 할거인지 입력해주세요 ." << endl;
 cout << "1 : 가장 쉬운 난이도 , 10 : 가장 어려운 난이도 " << endl;
 cin >> level;
 leveltime = 25*(11-level);

 headx = rand()%70;
 heady = rand()%23;
 foodx = rand()%70;
 foody = rand()%23;
 arr[foodx][foody].type = 3;

 while(1)
 {
  Sleep(leveltime);
  system("cls");

  if(_kbhit())
  {
   type = _getch();
   headWay(type, &headx, &heady); 
  } //방향키를 누른경우
  else
  {
   headWay(type, &headx, &heady);
  } //방향키를 누르지 않은 경우

  arr[headx][heady].type += 1;
  arr[headx][heady].time = count;

  //먹이는 숫자 3임
  cout << "위 : U, 아래 : J, 왼쪽 : H, 오른쪽 : K  <난이도" << level << ">"<<endl;

  for(int b = 0 ; b <23; b++)
  {
   for(int a=0; a<70; a++)
   {
    if(arr[a][b].type == 0)
     cout << " "; //그냥배경
    if(arr[a][b].type == 1)
    {
     cout << "o";
     arr[a][b].time --;
     if(arr[a][b].time == 0)
      arr[a][b].type = 0;
    } //그냥 뱀몸통
    if(arr[a][b].type == 2)
    {
     system("cls");
     cout << "Your Score is " << count -1 << endl;
     cout << "END GAME " <<endl;
     Sleep(10000);
     return 0;
    } //뱀이 자신의 몸에 닿아 게임이 끝난 경우
    if(arr[a][b].type == 3)
    {
     cout <<"X";
    } //그냥 먹이가 놓여진경우
    if(arr[a][b].type == 4)
    {
     arr[a][b].type = 1;
     count++;
     foodx = rand()%20;
     foody = rand()%20;
     arr[foodx][foody].type = 3;
    } //뱀이 먹이를 먹은경우
   }
   cout << endl;
  }
 }
 return 0;
}

void headWay(int type, int *x, int *y)
{   
 switch(type)
 {
 case 117:
  if(*y == 0)
   *y = 22;
  else
   *y = *y - 1;
  break;//위로
 case 106:
  if(*y == 22)
   *y = 0;
  else
   *y = *y + 1;
  break; // 아래로
 case 104:
  if(*x == 0)
   *x = 69;
  else
   *x = *x - 1;
  break; //왼쪽으로
 case 107:
  if(*x == 69)
   *x = 0;
  else
   *x = *x + 1;
  break; //오른쪽으로
 }
} //방향키를 눌럿을때 뱀이 움직이는 방향 조절하는 함수

'Code > c/c++' 카테고리의 다른 글

[C++]연산자오버로딩  (0) 2012.02.11
[C++]Double Linked List 기본기능만  (0) 2011.12.23
[C++]poker  (0) 2011.09.03
[Code] 유클리드 알고리즘(Euclid Algorism)  (0) 2011.08.29
[C++ Code]Swap  (0) 2011.07.28
Posted by I_co
2011. 9. 3. 14:55


/*
2011.05.19-20.
The Game Of Poker For Socket
Daun...
*/
/*

-카드 -
1~13 : 하트
14~26 : 다이아
27~39 : 스페이드
40~52 : 크로바

-무늬-
1:하트
2:다이아
3:스페이드
4:크로바

-숫자-

1~10 : 1~10
11 : J
12 : Q
13 : K
*/

#include <iostream>
#include <time.h>
using namespace std;

int main()
{
 srand(time(NULL));
 int myRandCard[5]={0}, num[13]={0}, shape[4]={0};

 cout << " J : 11, Q : 12 , K : 13"<<endl;
 cout << "shape[0] : ♥  shape[1] : ◆ shape[2] : ♠ shape[3] : ♣"<<"\n\n";

 while(1)
 {
  for (int i = 0 ; i < 5 ; i++)
   myRandCard[i]=rand()%52+1;

  int count=0;

  for(int i = 0 ; i < 5 ; i++)
   for(int a = i+1 ; a < 5 ; a++)
    if(myRandCard[a]==myRandCard[i])
     count++;

  if(count ==0)
   break;

 } //랜덤으로 카드를 뽑음!


 for (int i = 0 ; i < 5 ; i++)
 {
  if( myRandCard[i]/13 == 0)
  {
   shape[0]++;
   num[myRandCard[i]%13]++;
   cout << "뽑은 카드는 ♥" << myRandCard[i]%13 +1<<"입니다"<<endl;
  }
  else if(myRandCard[i]/13 == 1)
  {  
   shape[1]++;
   num[myRandCard[i]%13]++;
   cout << "뽑은 카드는 ◆"<<myRandCard[i]%13 +1 << "입니다."<<endl;
  }
  else if(myRandCard[i]/13 == 2)
  {
   shape[2]++;
   num[myRandCard[i]%13]++;
   cout << "뽑은 카드는 ♠"<<myRandCard[i]%13 +1 << "입니다."<<endl;
  }
  else
  {
   shape[3]++;
   num[myRandCard[i]%13]++;
   cout <<"뽑은 카드는 ♣"<<myRandCard[i]%13 +1 << "입니다."<<endl;
  }
 } //카드의 무늬 판별!+카드보여주기->저거 뽑은카드 숫자보여주는거 따로 함수써서 하고싶당! 13같은거는 K로 나타나도록!

// for(int i = 0 ; i < 4 ; i++)
//  cout << "shape[" << i << "] : " << shape[i] << "\t";
//
// cout << endl;
//
// for(int i = 0 ; i < 13 ; i++)
//  cout << "num[" << i+1 << "] : " << num[i] << "\t"; //카드 숫자새는거 제대로 된거인지 확인용이엿음!!

 int two=0, three=0, four=0;

 for(int i = 0 ; i < 13 ; i++)
 {
  if(num[i]==2)
   two++;
  else if(num[i]==3)
   three++;
  else if(num[i]==4)
   four++;
 }

 int flush = 0;

 for(int i = 0 ; i < 4 ; i++)
  if(shape[i]==5)
   flush++;


 cout << endl;


 if(four ==1)
  cout << "포카드 (Four Card)"<<endl;
 else if(three == 1 && two == 1)
  cout << "풀하우스 (Full house)"<<endl;
 else if(flush == 1 )
  cout << "플러쉬 (Flush)"<<endl;
 else if(three ==1)
  cout << "트리플(Triple)"<<endl;
 else if(two == 2)
  cout << "투페어(Two Pair)"<<endl;
 else if(two == 1)
  cout << "원페어(One Pair)"<<endl;
 else
 {
  int sum = 0;

  if(num[0]==1)
  {
   for(int i = 0 ; i < 5 ; i++)
    if(num[i]==1)
     sum++;
   if(sum == 5)
    cout << "백스트레이트(Back Straight)"<<endl;
  }

  sum = 0 ;

  if(num[0]==1)
  {
   for(int i = 9 ; i < 13 ; i++)
    if(num[i]==1)
     sum++;

   if(sum==4)
    cout << "마운틴 (Mountain)"<<endl;
  }

  else
  {
   for (int i = 1 ; i < 9 ; i ++)
   {
   sum = 0 ;
   for(int a = i ; a < i + 5 ; a++)
    if(num[a]==1)
     sum++;

   if(sum == 5)
    cout << "스트레이트 (Straight)" <<endl;
   }
  }
 }
 return 0;
}

'Code > c/c++' 카테고리의 다른 글

[C++]Double Linked List 기본기능만  (0) 2011.12.23
[C++]Game of Snake.  (0) 2011.09.03
[Code] 유클리드 알고리즘(Euclid Algorism)  (0) 2011.08.29
[C++ Code]Swap  (0) 2011.07.28
[C++ Code]주소값출력하기  (0) 2011.07.28
Posted by I_co
2011. 9. 3. 14:54


/*
2011.05.22-28.
Daun.

Client For Poker..
*/
#pragma comment(lib,"ws2_32.lib")
#include <winsock2.h>
#include <stdio.h>
#include <math.h>
#include <string.h>

#define PORT 8494
#define IP "192.168.0.36"

#define OFF 0
#define ON 1

char *SHAPE[4] = {"♤", "◇", "♡", "♧"};
char *NUM[13] = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
enum {BLACK,D_BLUE,D_GREEN,D_SKYBLUE,D_RED,D_VIOLET,D_YELLOW,GRAY,D_GRAY,BLUE,GREEN,SKYBLUE,RED,VIOLET,YELLOW,WHITE,};

void SetColor(int backcolor, int fontcolor)
{
 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), backcolor*16+fontcolor);
}


int myCard[5]={0};  //나의 카드 저장배열
int winner = 0;

int player0Card[5]={0}; //다른player의 카드 저장배열
int player1Card[5]={0}; //다른player의 카드 저장배열
int player2Card[5]={0}; //다른player의 카드 저장배열
int player3Card[5]={0}; //다른player의 카드 저장배열
int player4Card[5]={0}; //다른player의 카드 저장배열
int playerCard[5][5]={0};

int playerMoney[5]={20000000};
int allMoney = 0;
int willgiveMoney=100;

int menu=0;//선택매뉴를 저장하는 변수
int cardTurn = 0; //공개한 카드의 갯수를 저장함 0은 어처피 비공개라서...
int myPlayerNumber=0;
int die = OFF; //0인경우는 다이가 아닌경우 1로 변하면 다이가 되어있는경우.

int itemp=0; //int형 임시 저장소
char ctemp=0; //char형 임시 저장소

SOCKET soc; //소켓변수
struct sockaddr_in addr; //구조체변수

char buf[256]={0}; //전달할 문자열을 저장할 배열

void show();
void print_card(int);
void display_card(int , int);
void playGame(int);
void betting(int,int);
void printMoney(int );

void main()
{
 system("COLOR F0");
 SetColor(WHITE,BLACK);
 WSADATA wsa;
 WSAStartup(MAKEWORD(2,0),&wsa);

 soc = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

 addr.sin_addr.S_un.S_addr=inet_addr(IP);
 addr.sin_family=AF_INET;
 addr.sin_port=htons(8494);
 SetColor(WHITE,VIOLET);
 printf("\n\n\n\n\n\n\nWaiting~");
 SetColor(WHITE,BLACK);
 connect(soc,(struct sockaddr*)&addr, sizeof(addr));

 memset(buf,0,256);
 recv(soc,buf,256,0);
 sscanf(buf,"%d",&myPlayerNumber);

 die = OFF;
 memset(buf,0,256);
 recv(soc,buf,256,0); //나의 카드 목록을 받음
 sscanf(buf, "%d %d %d %d %d",&myCard[0],&myCard[1],&myCard[2],&myCard[3],&myCard[4]); //나의 카드를 배열에 저장

 system("cls");
 SetColor(WHITE,VIOLET);
 printf("당신은 %d번 플레이어입니다.\n",myPlayerNumber);
 printf("게임이 시작되었습니다.\n\n");
 SetColor(WHITE,BLACK);
 printf("\t\t받은 카드는 ");
 print_card(myCard[0]);
 print_card(myCard[1]); //받은 카드중 2개를 공개
 printf("입니다.\n\n");

 while(1)
 {
  fflush(stdin);
  memset(buf,0,256);
  recv(soc, buf, 256, 0);
  menu = buf[0];
  playGame(menu);
  if(menu == '6')
   break;
 }

 WSACleanup();
 closesocket(soc);
}

void print_card(int card)
{
 int shape = card / 13;
 int num = card % 13;
 if(shape==1||shape==2)
  SetColor(WHITE,RED);
 printf("%s%s ", SHAPE[shape], NUM[num]);
 SetColor(WHITE,BLACK);
} //숫자로 저장된 카드를 모양으로 바꾸어주는 함수


void display_card(int card[5], int num)
{
 num++;
 for(int i = 0 ; i<=num; i++)
  if(card[i] != 99)
   print_card(card[i]);
  else
   printf(" XX ");
 puts("");
} //카드목록을 보여주는 함수


void show()
{
 Sleep(1000);   
 system("cls");
 SetColor(WHITE,VIOLET);
 printf("당신은 %d번 플레이어입니다.\n\n\n",myPlayerNumber);
 SetColor(WHITE,BLACK);
 for(int i = 0 ; i < 5 ; i++)
 {
  if(i==myPlayerNumber)
  {
   printf("\t\t나의 카드는 : ");
   if(menu =='3')
    display_card(myCard,cardTurn+1);
   else
    display_card(myCard,cardTurn);
  }
  else
  {
   printf("\t\tplayer%d의 카드는 : ",i);
   display_card(playerCard[i],cardTurn-1);
  }
 }

}

void playGame(int menu)
{
 switch(menu)
 {
 case '1':
  if(die == OFF)
  {
   while(1)
   {
    printf("배팅을 할 차례입니다.\n (콜 : 1번, 하프 : 2번, 다이 : 0번)");
    printf("\n콜 : %d  , 하프 : %d\n\n",willgiveMoney,allMoney/2);
    fflush(stdin);
    scanf("%c",&ctemp);
    if(ctemp == '1' || ctemp == '2' || ctemp == '0')
     break;
   }

   if(ctemp == '0')
    die = ON;

   memset(buf,0,256);
   sprintf(buf,"%c",ctemp);
   send(soc,buf,256,0);
  }
  else
  {
   printf("현재 다이 상태 입니다.\n");
   fflush(stdin);
   memset(buf,0,256);
   sprintf(buf,"%c",'0');
   send(soc,buf,256,0);
  }
  //나의 배팅정보를 보냄
  break;

 case '2':
  sscanf(buf, "%d %d %d %d %d %d",&menu,&playerCard[0][cardTurn],&playerCard[1][cardTurn],&playerCard[2][cardTurn],&playerCard[3][cardTurn],&playerCard[4][cardTurn]);//각 플레이어의 2번재 카드를 저장
  if(die == OFF)
   show();
  else
  {   
   Sleep(1000);
   system("cls");
   printf("다이상태입니다. \n게임이 끝날때까지 기다리숑!");
  }
  cardTurn++;
  //카드를 받음
  break;

 case '3':
  cardTurn=2;
  show();
  //마지막카드확인
  break;

 case '5':
  int a, b;
  sscanf(buf,"%d %d %d",&menu,&a,&b);

  betting(a,b);

  if(b == 0)
   for(int i = 0 ; i < 5 ; i++)
    if(a == i)
     for(int p = 0 ; p < cardTurn; p++)
      playerCard[a][i] = 99;
  fflush(stdin);
  //memset(buf,0,256);
  recv(soc, buf, 256,0);
  sscanf(buf,"%d %d %d %d %d %d %d",&allMoney, &willgiveMoney, &playerMoney[0], &playerMoney[1], &playerMoney[2], &playerMoney[3], &playerMoney[4]);

  //배팅을한결과보여줌
  break;

 case '6':
   for(int i = 0 ; i < 5 ; i++)
   {
    sscanf(buf,"%d %d %d %d %d %d %d %d",&menu, &itemp, &playerCard[i][0],&playerCard[i][1],&playerCard[i][2],&playerCard[i][3],&playerCard[i][4],&winner);
    memset(buf,0,256);
    if(i != 4)
    recv(soc, buf, 256, 0);
   }
   for(int i = 0 ; i < 5 ; i++)
   {
    printf("player %d  : ",i);
    display_card(playerCard[i],3);
    puts("");
   }
   printf("\n승자는 : %d player 입니다.\n",winner);
   Sleep(10000);
   break;

 case '9':
  while(1)
  {
   printf(" 어떤카드를 공개하겠습니까?(왼쪽카드 : 0, 오른쪽카드 : 1) : ");
   fflush(stdin);
   scanf("%d",&itemp); //공개할 카드 번호를 선택
   if(itemp == 0 || itemp == 1)
    break;
  }
  if(itemp==0)
   sprintf(buf,"%d",myCard[0]);
  else
   sprintf(buf,"%d",myCard[1]);

  send(soc,buf,256,0); //공개할 카드 번호를 보냄
  break;
 }
}


void betting(int player ,int sel)
{
 switch(sel)
 {
 case 1:
  printf("\n\n%d번 플레이어가 콜을 했습니다.\n",player);
  printMoney(player);
  break;
 case 2:
  printf("\n\n%d번 플레이어가 하프를 했습니다.\n",player);
  printMoney(player);
  break;
 case 0:
  printf("\n\n%d번 플레이어가 다이를 했습니다.\n",player);
  printMoney(player);
  break;

 }
}

void printMoney(int player)
{
 printf("\n%d번 플레이어의 남은 돈은 %d원 입니다.\n\n",player,playerMoney[player]);
}

'Code > socket' 카테고리의 다른 글

[SOCKET]1:1 chatting server  (0) 2011.09.03
[SOCKET]1:1 chating client  (0) 2011.09.03
[SOCKET]chating server for test  (0) 2011.09.03
[SOCKET]chating client for test  (0) 2011.09.03
Posted by I_co
2011. 9. 3. 14:53


/*
2011.05.17.-18.
Daun.
1:1chatting.
Server..
*/
#include <winsock2.h>
#include <stdio.h>
#include<iostream>

#include<process.h> //스레드
#include<windows.h> //스레드

#include<time.h> //sleep 사용시

 


#define PORT 3000;
 char buf[256]={0};
 SOCKET soc, soc_client;


unsigned WINAPI rThreadFunction(void* arg);


unsigned WINAPI sThreadFunction(void* arg);

 

void main()
{
 WSADATA wsa;


 struct sockaddr_in addr;
 int size;

 HANDLE rThread, sThread;

 int intThread=0;
 WSAStartup(MAKEWORD(2,0),&wsa);

 soc = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);

 

 addr.sin_family=AF_INET;
 addr.sin_port=htons(3000);
 addr.sin_addr.S_un.S_addr=htonl(INADDR_ANY);

 bind(soc,(struct sockaddr*)&addr, sizeof(addr));

 listen(soc, 5);

 size = sizeof(addr);

 soc_client = accept(soc, (struct sockaddr*)&addr, &size);


 while(1)
 {
 rThread=(HANDLE)_beginthreadex(0,0,rThreadFunction,0,0,(unsigned*)intThread);
 sThread=(HANDLE)_beginthreadex(0,0,sThreadFunction,0,0,(unsigned*)intThread);
}
 TerminateThread(rThread,0);
 TerminateThread(sThread,0);

 

 closesocket(soc);
 WSACleanup();
}

unsigned WINAPI rThreadFunction(void* arg)
{
 for(int i=1;i<=30;i++)
 {
 recv(soc_client, buf, 256,0);
 printf("상대방 : %s\n",buf);
 }
 return 0;
}


unsigned WINAPI sThreadFunction(void* arg)
{
 for(int i=1;i<=30;i++)
 {
  
 scanf("%s",&buf);
 send(soc_client, buf, 256, 0);


 }
 return 0;
}

'Code > socket' 카테고리의 다른 글

[SOCKET] clinet for poker  (0) 2011.09.03
[SOCKET]1:1 chating client  (0) 2011.09.03
[SOCKET]chating server for test  (0) 2011.09.03
[SOCKET]chating client for test  (0) 2011.09.03
Posted by I_co
2011. 9. 3. 14:53

/*
2011.05.17.-18.
Daun.
1:1Chatting
Client..
*/
#include <winsock2.h>
#include <stdio.h>
#include<iostream>

#include<process.h> //스레드
#include<windows.h> //스레드

#include<time.h> //sleep 사용시

 

#define PORT 3000
 char buf[256]={0};
  SOCKET soc;

 

unsigned WINAPI rThreadFunction(void* arg);

unsigned WINAPI sThreadFunction(void* arg);

void main()
{


 struct sockaddr_in addr;
 HANDLE rThread, sThread;

 int intThread=0;

 WSADATA wsa;
 WSAStartup(MAKEWORD(2,0),&wsa);

 soc = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

 addr.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");
 addr.sin_family=AF_INET;
 addr.sin_port=htons(3000);

 connect(soc,(struct sockaddr*)&addr, sizeof(addr));
while(1)
{
 sThread=(HANDLE)_beginthreadex(0,0,sThreadFunction,0,0,(unsigned*)intThread);
 rThread=(HANDLE)_beginthreadex(0,0,rThreadFunction,0,0,(unsigned*)intThread);
}
 TerminateThread(rThread,0);
 TerminateThread(sThread,0);

 

 
 WSACleanup();
 closesocket(soc);

}


unsigned WINAPI rThreadFunction(void* arg)
{
 for(int i=1;i<=30;i++)
 {
 recv(soc, buf, 256,0);
 printf("상대방 : %s\n",buf);
 }
 return 0;
}


unsigned WINAPI sThreadFunction(void* arg)
{
 for(int i=1;i<=30;i++)
 {
  
 scanf("%s",&buf);
 send(soc, buf, 256, 0);


 }
 return 0;
}

'Code > socket' 카테고리의 다른 글

[SOCKET] clinet for poker  (0) 2011.09.03
[SOCKET]1:1 chatting server  (0) 2011.09.03
[SOCKET]chating server for test  (0) 2011.09.03
[SOCKET]chating client for test  (0) 2011.09.03
Posted by I_co
2011. 9. 3. 14:52


/*
2011.05.17.-18.
Daun.

Server..
*/
#include <winsock2.h>
#include <stdio.h>


#define PORT 3000;

void main()
{
 WSADATA wsa;

 SOCKET soc, soc_client;
 struct sockaddr_in addr;
 char buf[256]={0};
 int size;

 WSAStartup(MAKEWORD(2,0),&wsa);

 soc = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);

 

 addr.sin_family=AF_INET;
 addr.sin_port=htons(3000);
 addr.sin_addr.S_un.S_addr=htonl(INADDR_ANY);

 bind(soc,(struct sockaddr*)&addr, sizeof(addr));

 listen(soc, 5);

 size = sizeof(addr);

 soc_client = accept(soc, (struct sockaddr*)&addr, &size);

 while(1)
 {
  recv(soc_client, buf, 256,0);
  printf("%s",buf);
  fflush(stdin);
  memset(buf,0,256);
  puts("");
  scanf("%s",buf);
  send(soc_client, buf, 256, 0);
  fflush(stdin);
  memset(buf,0,256);
  puts("");

 }

 closesocket(soc);
 WSACleanup();
}

 

 

 

'Code > socket' 카테고리의 다른 글

[SOCKET] clinet for poker  (0) 2011.09.03
[SOCKET]1:1 chatting server  (0) 2011.09.03
[SOCKET]1:1 chating client  (0) 2011.09.03
[SOCKET]chating client for test  (0) 2011.09.03
Posted by I_co
2011. 9. 3. 14:51


/*
2011.05.17.-18.
Daun.

Client..
*/
#include <winsock2.h>
#include <stdio.h>

#define PORT 3000

void main()
{

 SOCKET soc;
 struct sockaddr_in addr;
 char buf[256]={0};
 WSADATA wsa;
 WSAStartup(MAKEWORD(2,0),&wsa);

 soc = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

 addr.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");
 addr.sin_family=AF_INET;
 addr.sin_port=htons(3000);

 connect(soc,(struct sockaddr*)&addr, sizeof(addr));

 while(1)
 {
  scanf("%s",&buf);
  send(soc, buf,256,0);
  fflush(stdin);
  memset(buf,0,256);
  puts("");
  recv(soc, buf,256,0);
  printf("%s",&buf);
  fflush(stdin);
  memset(buf,0,256);
  puts("");
 }


 WSACleanup();
 closesocket(soc);

}

 

'Code > socket' 카테고리의 다른 글

[SOCKET] clinet for poker  (0) 2011.09.03
[SOCKET]1:1 chatting server  (0) 2011.09.03
[SOCKET]1:1 chating client  (0) 2011.09.03
[SOCKET]chating server for test  (0) 2011.09.03
Posted by I_co