jeu de bataille navale, pouvant se jouer seul contre le pc ou en réseau local ou internet. Il est doté d'un chat qui permet de communiquer avec son adversaire.
Il reste à mettre des images plus sympas, et à enrichier le jeu de quelques animations pour l'améliorer. Le but est de le laisser gratuit.
Source / Exemple :
#include "windows.h"
#include "boot.h"
#include "winsock.h"
#include "stdlib.h"
#include "time.h"
#include "erreur.h"
#include "mmsystem.h"
#define WM_SOCKET (WM_USER+1)
LRESULT CALLBACK WindowProc(HWND , UINT, WPARAM, LPARAM);
BOOL CALLBACK AboutDlgProc(HWND,UINT,WPARAM,LPARAM);
BOOL CALLBACK MessDlgProc(HWND,UINT,WPARAM,LPARAM);
BOOL CALLBACK ClieDlgProc(HWND,UINT,WPARAM,LPARAM);
BOOL CALLBACK ServDlgProc(HWND,UINT,WPARAM,LPARAM);
void messageZ(HWND,LPSTR);
void zero(int*,int*,int*,int*,int*);
void freshB (HWND); //rafraississt de zone select bato
void freshGrilG( HWND hwnd); //rafraichisst grille gauche
void freshGrilD( HWND hwnd); //idem grille droite
void freshCaseG(HWND hwnd,int i,int j);//idem pour 1 case grille g
void freshCaseD(HWND hwnd,int i,int j);// " " " d
void freshTorp(HWND hwnd); //" feu torpille
void purgtab(int a); //pour effacer les bateaux non positionnés
int scanCorp(int nbcorp, int a,int b,HWND hwnd); //vérifie si cases sont libres pour placer un bateau
void construct(int nbcorp); //met des "1" pour le corps du bateau
void putenemi(int nbcorp);//pour construction bato ennemi
int scanEnemi( int nbcorp);//sous fct de putenemi
void constrEnemi(int nbcorp);//sous fct de putenemi
void reinit();//pour reinitialiser partie
void combatsolo(HWND,int,int);//fonction de process combat solo
void bords();//sous fct combatsolo
void bords2();// idem
BOOL optimiz();// idem
//fonction réseau
void ShoErr(LPSTR,int); //pour visualiser le type d'erreur qui est arrivé
void initsock(); //initialise winsock
int verif(); //vérifie si winsock est initialisée
void nomhote(); //donne le nom de l'hote
BOOL serveur(HWND,HWND);
BOOL client(HWND,HWND);
void cleanNet();//pour tout nettoyer avant fermeture prog
BOOL liAddr();//pour lire l'adresse entrée par le client
void Angriff(HWND);//procédure de jeu en réseau
void choiprem(HWND);//pour déterminer qui joue en premier
DWORD playMIDIFile(HWND hWndNotify, LPSTR lpszMIDIFileName);
int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.style=CS_HREDRAW|CS_VREDRAW;
wndclass.lpfnWndProc=WindowProc;
wndclass.cbClsExtra=0;
wndclass.cbWndExtra=0;
wndclass.hInstance=hInstance;
wndclass.hIcon=LoadIcon(hInstance,"fleur");
wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
wndclass.hbrBackground=(HBRUSH)GetStockObject(GRAY_BRUSH);
wndclass.lpszMenuName=szAppName;
wndclass.lpszClassName=szAppName;
if(!RegisterClass(&wndclass))
{
MessageBox(NULL,"Windows 95 requis","machin",MB_OK|MB_ICONSTOP);
return 0;
}
hwnd=CreateWindow(szAppName,
"DAS BOOT",
WS_CAPTION|WS_SYSMENU,
CW_USEDEFAULT,
CW_USEDEFAULT,
600,
400,
NULL,
NULL,
hInstance,
NULL);
ShowWindow(hwnd,nShowCmd);
UpdateWindow(hwnd);
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static HINSTANCE hInstance;
static HBITMAP hBit1,hBit2,hBit3,hBit4,hBit5,hBit6,hBit7,hBit8,hBit9,hBit10,hBit11,hBit12;
static HDC hdc,hdcMem1,hdcMem2,hdcMem3,hdcMem4,hdcMem5,hdcMem6,hdcMem7,hdcMem8,hdcMem9,hdcMem10,hdcMem11,hdcMem12;
//essai affichage victoire
static HDC hdcVic;
static HBITMAP hBitVic;
TEXTMETRIC tm;
HBRUSH hBrush;
HPEN hPen, hPenOld;
LOGBRUSH lb;
RECT rct;
int i,j;
DLGPROC dlgprc,dlgmess,dlgclie,dlgserv;
HWND hDlg;
HOSTENT * invite;
char adre[64];
unsigned int ad;
switch (message)
{
case WM_CREATE:
hMenu=GetMenu(hwnd);
EnableMenuItem(hMenu,IDM_MESS,MF_GRAYED);
hInstance=((LPCREATESTRUCT)lParam)->hInstance;
hBit1=LoadBitmap(hInstance,"portah");
hBit2=LoadBitmap(hInstance,"portav");
hBit3=LoadBitmap(hInstance,"bato4h");
hBit4=LoadBitmap(hInstance,"bato4v");
hBit5=LoadBitmap(hInstance,"bato3h");
hBit6=LoadBitmap(hInstance,"bato3v");
hBit7=LoadBitmap(hInstance,"bato2h");
hBit8=LoadBitmap(hInstance,"bato2v");
hBit9=LoadBitmap(hInstance,"soum3h");
hBit10=LoadBitmap(hInstance,"soum3v");
hBit11=LoadBitmap(hInstance,"b2bat3h");
hBit12=LoadBitmap(hInstance,"b2bat3v");
//essai affichage victoire
hBitVic=LoadBitmap(hInstance,"victoire");
hdcVic=CreateCompatibleDC(hdc);
SelectObject(hdcVic,hBitVic);
hdcMem1=CreateCompatibleDC(hdc);
SelectObject(hdcMem1,hBit1);
hdcMem2=CreateCompatibleDC(hdc);
SelectObject(hdcMem2,hBit2);
hdcMem3=CreateCompatibleDC(hdc);
SelectObject(hdcMem3,hBit3);
hdcMem4=CreateCompatibleDC(hdc);
SelectObject(hdcMem4,hBit4);
hdcMem5=CreateCompatibleDC(hdc);
SelectObject(hdcMem5,hBit5);
hdcMem6=CreateCompatibleDC(hdc);
SelectObject(hdcMem6,hBit6);
hdcMem7=CreateCompatibleDC(hdc);
SelectObject(hdcMem7,hBit7);
hdcMem8=CreateCompatibleDC(hdc);
SelectObject(hdcMem8,hBit8);
hdcMem9=CreateCompatibleDC(hdc);
SelectObject(hdcMem9,hBit9);
hdcMem10=CreateCompatibleDC(hdc);
SelectObject(hdcMem10,hBit10);
hdcMem11=CreateCompatibleDC(hdc);
SelectObject(hdcMem11,hBit11);
hdcMem12=CreateCompatibleDC(hdc);
SelectObject(hdcMem12,hBit12);
hdc=GetDC(hwnd);
GetTextMetrics(hdc,&tm);
cxChar=tm.tmAveCharWidth;
cyChar=tm.tmHeight+tm.tmExternalLeading;
ReleaseDC(hwnd,hdc);
if((music==1)&&(flagson==0)){flagson=1;SendMessage(hwnd,WM_COMMAND,ID_MIDI,0);}
return 0;
case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
//dessin viseur
SelectObject(hdc,GetStockObject(NULL_BRUSH));
Ellipse(hdc,150,50,450,350);
MoveToEx(hdc,300,350,NULL);
LineTo(hdc,300,200);
for(i=0;i<10;i++)
{
MoveToEx(hdc,300-i*3,350-i*15,NULL);
LineTo(hdc,300+i*3,350-i*15);
}
for(i=0;i<8;i++)
{
MoveToEx(hdc,300-i*3,50+i*15,NULL);
LineTo(hdc,300+i*3,50+i*15);
}
MoveToEx(hdc,150,200,NULL);
LineTo(hdc,350,200);
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//dessin des 2 grilles
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
SetBkMode(hdc, TRANSPARENT);
SetTextAlign(hdc,TA_CENTER);
//remplissage cadres
lb.lbStyle = BS_SOLID;
lb.lbColor = RGB(0,0,255);
lb.lbHatch = HS_BDIAGONAL;
hBrush = CreateBrushIndirect(&lb);
SelectObject(hdc,hBrush);
rct.top=50;
rct.left=50;
rct.right=250;
rct.bottom=250;
FillRect(hdc,&rct,hBrush);
rct.top=50;
rct.left=350;
rct.right=550;
rct.bottom=250;
FillRect(hdc,&rct,hBrush);
lb.lbStyle = BS_SOLID;
lb.lbColor = RGB(100,100,100);
lb.lbHatch = HS_BDIAGONAL;
DeleteObject(hBrush);
hBrush = CreateBrushIndirect(&lb);
SelectObject(hdc,hBrush);
rct.top=290;
rct.left=50;
rct.right=250;
rct.bottom=330;
FillRect(hdc,&rct,hBrush);
rct.top=275;
rct.left=350;
rct.right=550;
rct.bottom=350;
FillRect(hdc,&rct,hBrush);
//textes
SetTextColor(hdc, RGB(255, 0, 0));
TextOut(hdc,450,20,"Ennemi",strlen("Ennemi"));
TextOut(hdc,150,20,"Vous",strlen("Vous"));
TextOut(hdc,450,255,"Messages",strlen("Messages"));
TextOut(hdc,150,290-cyChar,"Bateaux",strlen("Bateaux"));
SetTextColor(hdc, RGB(0, 0, 0));
for(i=0;i<=1;i++)
{
TextOut(hdc,(300*i)+60,50-cyChar,"1",1);
TextOut(hdc,(300*i)+80,50-cyChar,"2",1);
TextOut(hdc,(300*i)+100,50-cyChar,"3",1);
TextOut(hdc,(300*i)+120,50-cyChar,"4",1);
TextOut(hdc,(300*i)+140,50-cyChar,"5",1);
TextOut(hdc,(300*i)+160,50-cyChar,"6",1);
TextOut(hdc,(300*i)+180,50-cyChar,"7",1);
TextOut(hdc,(300*i)+200,50-cyChar,"8",1);
TextOut(hdc,(300*i)+220,50-cyChar,"9",1);
TextOut(hdc,(300*i)+240,50-cyChar,"10",2);
}
for(i=0;i<=1;i++)
{
TextOut(hdc,(i*300)+50-cxChar,50,"A",1);
TextOut(hdc,(i*300)+50-cxChar,70,"B",1);
TextOut(hdc,(i*300)+50-cxChar,90,"C",1);
TextOut(hdc,(i*300)+50-cxChar,110,"D",1);
TextOut(hdc,(i*300)+50-cxChar,130,"E",1);
TextOut(hdc,(i*300)+50-cxChar,150,"F",1);
TextOut(hdc,(i*300)+50-cxChar,170,"G",1);
TextOut(hdc,(i*300)+50-cxChar,190,"H",1);
TextOut(hdc,(i*300)+50-cxChar,210,"I",1);
TextOut(hdc,(i*300)+50-cxChar,230,"J",1);
}
//torpille--------------
SetTextAlign(hdc,TA_CENTER);
TextOut(hdc,300,0,"Torpedo",7);
SetTextAlign(hdc,TA_LEFT|TA_TOP|TA_NOUPDATECP);
Ellipse(hdc,290,20,310,40);
if(((solo==1)&&(tour==0)&&(finS==0))||((reso==1)&&(montour==1)&&(finR==0)))
{
hPen = CreatePen(PS_INSIDEFRAME, 8, RGB(0, 255, 0));
hPenOld = SelectObject(hdc, hPen);
SelectObject(hdc,hPen);
Ellipse(hdc,292,22,308,38); //remplissage
}
if(((solo==1)&&(tour==1)&&(finS==0))||((reso==1)&&(montour==0)&&(finR==0)))
{
hPen = CreatePen(PS_INSIDEFRAME, 8, RGB(255, 0, 0));
hPenOld = SelectObject(hdc, hPen);
SelectObject(hdc,hPen);
Ellipse(hdc,292,22,308,38); //remplissage
}
//SelectObject(hdc,GetStockObject(NULL_BRUSH));
SelectObject(hdc, hPenOld);
DeleteObject(hPen);
//fin torpille-----------------
//zone de texte (messages)
SetTextColor(hdc, RGB(255, 255, 0));
SetTextAlign(hdc,TA_LEFT);
TextOut(hdc,350,275,string1,strlen(string1));
TextOut(hdc,350,275+cyChar,string2,strlen(string2));
TextOut(hdc,350,275+cyChar*2,string3,strlen(string3));
TextOut(hdc,350,275+cyChar*3,string4,strlen(string4));
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//dessin des bateaux
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//jeu pas comencé : pas de bato
//jeu commencé : positionnement des bato : choix sur palette
if((solo==1)||(reso==1))
{
if(choiB1==0)BitBlt(hdc,50,290,100,20,hdcMem1,0,0,SRCCOPY);
if(choiB1==1)BitBlt(hdc,50,290,100,20,hdcMem1,0,0,NOTSRCCOPY);
if(choiB5==0)BitBlt(hdc,150,290,60,20,hdcMem5,0,0,SRCCOPY);
if(choiB5==1)BitBlt(hdc,150,290,60,20,hdcMem5,0,0,NOTSRCCOPY);
if(choiB7==0)BitBlt(hdc,210,290,40,20,hdcMem7,0,0,SRCCOPY);
if(choiB7==1)BitBlt(hdc,210,290,40,20,hdcMem7,0,0,NOTSRCCOPY);
if(choiB3==0)BitBlt(hdc,50,310,80,20,hdcMem3,0,0,SRCCOPY);
if(choiB3==1)BitBlt(hdc,50,310,80,20,hdcMem3,0,0,NOTSRCCOPY);
if(choiB11==0)BitBlt(hdc,130,310,60,20,hdcMem11,0,0,SRCCOPY);
if(choiB11==1)BitBlt(hdc,130,310,60,20,hdcMem11,0,0,NOTSRCCOPY);
if(choiB9==0)BitBlt(hdc,190,310,60,20,hdcMem9,0,0,SRCCOPY);
if(choiB9==1)BitBlt(hdc,190,310,60,20,hdcMem9,0,0,NOTSRCCOPY);
}
//positionnneme,t des bato sur grille gauche
for(i=0;i<10;i++)
for(j=0;j<10;j++)
{
if(monpay[i][j]==2)BitBlt(hdc,50+j*20,50+i*20,100,20,hdcMem1,0,0,SRCCOPY);
if(monpay[i][j]==3)BitBlt(hdc,50+j*20,50+i*20,20,100,hdcMem2,0,0,SRCCOPY);
if(monpay[i][j]==4)BitBlt(hdc,50+j*20,50+i*20,80,20,hdcMem3,0,0,SRCCOPY);
if(monpay[i][j]==5)BitBlt(hdc,50+j*20,50+i*20,20,80,hdcMem4,0,0,SRCCOPY);
if(monpay[i][j]==6)BitBlt(hdc,50+j*20,50+i*20,60,20,hdcMem5,0,0,SRCCOPY);
if(monpay[i][j]==7)BitBlt(hdc,50+j*20,50+i*20,20,60,hdcMem6,0,0,SRCCOPY);
if(monpay[i][j]==8)BitBlt(hdc,50+j*20,50+i*20,40,20,hdcMem7,0,0,SRCCOPY);
if(monpay[i][j]==9)BitBlt(hdc,50+j*20,50+i*20,20,40,hdcMem8,0,0,SRCCOPY);
if(monpay[i][j]==10)BitBlt(hdc,50+j*20,50+i*20,60,20,hdcMem9,0,0,SRCCOPY);
if(monpay[i][j]==11)BitBlt(hdc,50+j*20,50+i*20,20,60,hdcMem10,0,0,SRCCOPY);
if(monpay[i][j]==12)BitBlt(hdc,50+j*20,50+i*20,60,20,hdcMem11,0,0,SRCCOPY);
if(monpay[i][j]==13)BitBlt(hdc,50+j*20,50+i*20,20,60,hdcMem12,0,0,SRCCOPY);
}
//positionnneme,t des bato mals placéssur grille gauche
for(i=0;i<10;i++)
for(j=0;j<10;j++)
{
if(monpay3[i][j]==2)BitBlt(hdc,50+j*20,50+i*20,100,20,hdcMem1,0,0,SRCAND);
if(monpay3[i][j]==3)BitBlt(hdc,50+j*20,50+i*20,20,100,hdcMem2,0,0,SRCAND);
if(monpay3[i][j]==4)BitBlt(hdc,50+j*20,50+i*20,80,20,hdcMem3,0,0,SRCAND);
if(monpay3[i][j]==5)BitBlt(hdc,50+j*20,50+i*20,20,80,hdcMem4,0,0,SRCAND);
if(monpay3[i][j]==6)BitBlt(hdc,50+j*20,50+i*20,60,20,hdcMem5,0,0,SRCAND);
if(monpay3[i][j]==7)BitBlt(hdc,50+j*20,50+i*20,20,60,hdcMem6,0,0,SRCAND);
if(monpay3[i][j]==8)BitBlt(hdc,50+j*20,50+i*20,40,20,hdcMem7,0,0,SRCAND);
if(monpay3[i][j]==9)BitBlt(hdc,50+j*20,50+i*20,20,40,hdcMem8,0,0,SRCAND);
if(monpay3[i][j]==10)BitBlt(hdc,50+j*20,50+i*20,60,20,hdcMem9,0,0,SRCAND);
if(monpay3[i][j]==11)BitBlt(hdc,50+j*20,50+i*20,20,60,hdcMem10,0,0,SRCAND);
if(monpay3[i][j]==12)BitBlt(hdc,50+j*20,50+i*20,60,20,hdcMem11,0,0,SRCAND);
if(monpay3[i][j]==13)BitBlt(hdc,50+j*20,50+i*20,20,60,hdcMem12,0,0,SRCAND);
}
// -------------------------
//affichage croix et ronds
//---------------------------
//ronds blancs
//------------
hPen = CreatePen(PS_SOLID, 6, RGB(255, 255, 255));
hPenOld = SelectObject(hdc, hPen);
SelectObject(hdc,hPen);
SelectObject(hdc,GetStockObject(NULL_BRUSH));
//grille gauche
for(i=0;i<10;i++)
for(j=0;j<10;j++)
{
if(monpay2[i][j]==1)Ellipse(hdc, 53+j*20, 53+i*20, 47+j*20+20, 47+i*20+20);
}
//grille droite
for(i=0;i<10;i++)
for(j=0;j<10;j++)
{
if(ennemi2[i][j]==1)Ellipse(hdc, 353+j*20, 53+i*20, 347+j*20+20, 47+i*20+20);
}
SelectObject(hdc, hPenOld);
DeleteObject(hPen);
// croix rouges
//--------------
hPen = CreatePen(PS_SOLID, 6, RGB(255, 0, 0));
hPenOld = SelectObject(hdc, hPen);
SelectObject(hdc,hPen);
SelectObject(hdc,GetStockObject(NULL_BRUSH));
//grille gauche
for(i=0;i<10;i++)
for(j=0;j<10;j++)
{
if(monpay2[i][j]==2){MoveToEx(hdc,53+j*20,53+i*20,NULL);LineTo(hdc,50+j*20+17,50+i*20+17);
MoveToEx(hdc,53+j*20,50+i*20+17,NULL);LineTo(hdc,50+j*20+17,53+i*20);}
}
//grille droite
for(i=0;i<10;i++)
for(j=0;j<10;j++)
{
if(ennemi2[i][j]==2){MoveToEx(hdc,353+j*20,53+i*20,NULL);LineTo(hdc,350+j*20+17,50+i*20+17);
MoveToEx(hdc,353+j*20,50+i*20+17,NULL);LineTo(hdc,350+j*20+17,53+i*20);}
}
SelectObject(hdc, hPenOld);
DeleteObject(hPen);
//lignes
for(i=0;i<=10;i++)
{
MoveToEx(hdc,50,50+(i*20),NULL);
LineTo(hdc,250,50+(i*20));
MoveToEx(hdc,350,50+(i*20),NULL);
LineTo(hdc,550,50+(i*20));
}
for(i=0;i<=10;i++)
{
MoveToEx(hdc,50+(i*20),50,NULL);
LineTo(hdc,50+(i*20),250);
}
for(i=0;i<=10;i++)
{
MoveToEx(hdc,350+(i*20),50,NULL);
LineTo(hdc,350+(i*20),250);
}
//cadres messages et bateaux
MoveToEx(hdc,50,290,NULL);
LineTo(hdc,50,330);
MoveToEx(hdc,250,290,NULL);
LineTo(hdc,250,330);
MoveToEx(hdc,50,310,NULL);
LineTo(hdc,250,310);
MoveToEx(hdc,150,310,NULL);
LineTo(hdc,150,290);
MoveToEx(hdc,210,310,NULL);
LineTo(hdc,210,290);
MoveToEx(hdc,130,310,NULL);
LineTo(hdc,130,330);
MoveToEx(hdc,190,310,NULL);
LineTo(hdc,190,330);
//droite
MoveToEx(hdc,350,275,NULL);
LineTo(hdc,350,350);
MoveToEx(hdc,550,275,NULL);
LineTo(hdc,550,350);
MoveToEx(hdc,50,290,NULL);
LineTo(hdc,250,290);
MoveToEx(hdc,50,330,NULL);
LineTo(hdc,250,330);
//droite
MoveToEx(hdc,350,275,NULL);
LineTo(hdc,550,275);
MoveToEx(hdc,350,350,NULL);
LineTo(hdc,550,350);
//affichage suite à victoire
SetTextAlign(hdc,TA_CENTER);
if((sourFlag==1)&&((scoreR==1)||(aff==1))){StretchBlt(hdc,0,0,600,400,hdcVic,0,0,300,200,SRCCOPY);
SetBkMode(hdc, TRANSPARENT);
SetTextColor(hdc,RGB(0,0,0));
TextOut(hdc,299,199,"VOUS AVEZ GAGNE. RETOUR AU PORT",31);
SetTextColor(hdc,RGB(255,255,255));
TextOut(hdc,300,200,"VOUS AVEZ GAGNE. RETOUR AU PORT",31);}
SetTextAlign(hdc,TA_LEFT|TA_TOP|TA_NOUPDATECP);
EndPaint(hwnd,&ps);
return 0;
//paint
case WM_COMMAND:
hdc=GetDC(hwnd);
switch (LOWORD (wParam))
{
case IDM_ABOUT:
dlgprc =MakeProcInstance( (DLGPROC)AboutDlgProc, hInstance);
DialogBox(hInstance,"BOX", hwnd, (DLGPROC)AboutDlgProc);
FreeProcInstance(dlgprc);
return 0;
case IDM_MESS:
dlgmess =MakeProcInstance( (DLGPROC)MessDlgProc, hInstance);
DialogBox(hInstance,"ENVOI", hwnd, (DLGPROC)MessDlgProc);
FreeProcInstance(dlgmess);
return 0;
case IDM_CLIE:
if((cli==1)||(serv==1)||(solo==1)){if(IDYES==MessageBox(hwnd,"Voulez vous vraiment commencer une nouvelle partie ?","Recommencer ????",MB_YESNO|MB_ICONQUESTION))
{reinit();cleanNet();InvalidateRect(hwnd,NULL,TRUE);cli=1;}}
if(cli==1){if(IDYES==MessageBox(hwnd,"Voulez vous vraiment commencer une nouvelle partie ?","Recommencer ????",MB_YESNO|MB_ICONQUESTION))
{reinit();InvalidateRect(hwnd,NULL,TRUE);cli=1;}}
dlgclie =MakeProcInstance( (DLGPROC)ClieDlgProc, hInstance);
DialogBox(hInstance,"CLIE", hwnd, (DLGPROC)ClieDlgProc);
FreeProcInstance(dlgclie);
return 0;
case IDM_SERV:
if((cli==1)||(serv==1)||(solo==1)){if(IDYES==MessageBox(hwnd,"Voulez vous vraiment commencer une nouvelle partie ?","Recommencer ????",MB_YESNO|MB_ICONQUESTION))
{reinit();cleanNet();InvalidateRect(hwnd,NULL,TRUE);serv=1;}}
dlgserv =MakeProcInstance( (DLGPROC)ServDlgProc, hInstance);
DialogBox(hInstance,"SERV", hwnd, (DLGPROC)ServDlgProc);
FreeProcInstance(dlgserv);
return 0;
case ID_KEEP:
if((cli==0)&&(serv==0)){MessageBox(hwnd,"Vous devez être connecté à un autre joueur.","Impossible",MB_OK);return 0;}
if((cli==1)||(serv==1))
{
if(IDYES==MessageBox(hwnd,"Voulez vous vraiment commencer une nouvelle partie ?","Recommencer ????",MB_YESNO|MB_ICONQUESTION))
{
for(i=0;i<150;i++)buffer[i]=0;
buffer[135]=1;
//réinitialiser l'autre
if(serv==1){iSendLen=send(Sinv,buffer,sizeof(buffer),0);if(iSendLen==0){ShoErr("send",WSAGetLastError());return 0;}}//warzaza
if(cli==1){iSendLen=send(Scli,buffer,sizeof(buffer),0);if(iSendLen==0){ShoErr("send",WSAGetLastError());return 0;}}
//se réinitialiser soi même
keep=1;
reinit();
keep=0;
reso=1;
messageZ(hwnd,"Sélectionnez un bateau du bouton gauche de la souris");
InvalidateRect(hwnd,NULL,TRUE);
UpdateWindow(hwnd);
return 0;
}
}
return 0;
case IDM_QUIT:
SendMessage(hwnd,WM_DESTROY,0,0);
return 0;
case IDM_SOLO:
if((cli==1)||(serv==1)||(solo==1)){if(IDYES==MessageBox(hwnd,"Voulez vous vraiment commencer une nouvelle partie ?","Recommencer ????",MB_YESNO|MB_ICONQUESTION))
{reinit();cleanNet();InvalidateRect(hwnd,NULL,TRUE);solo=1;}}
solo=1;
reso=0;
messageZ(hwnd,"Vous avez choisi de jouer contre le PC. Sélectionnez un bateau dans le cadre ci-contre");
freshB(hwnd);
return 0;
case IDM_ADDR:
if(soc==0)initsock();
nomhote();
return 0;
case ID_SON:
if(son==0){son=1;CheckMenuItem(hMenu,ID_SON,MF_CHECKED);return 0;}
if(son==1){son=0;CheckMenuItem(hMenu,ID_SON,MF_UNCHECKED);return 0;}
return 0;
case ID_MUSIC:
if(music==0){music=1;CheckMenuItem(hMenu,ID_MUSIC,MF_CHECKED);playMIDIFile(hwnd, "song.mid");return 0;}
if(music==1){music=0;CheckMenuItem(hMenu,ID_MUSIC,MF_UNCHECKED);mciSendCommand(wDeviceID, MCI_CLOSE, 0, 0);flagson=0;return 0;}
return 0;
case ID_MIDI:
playMIDIFile(hwnd, "song.mid");
return 0;
break;
}
ReleaseDC(hwnd,hdc);
return 0;
case MM_MCINOTIFY:
flagson=0;
if((music==1))
{
mciSendCommand(wDeviceID,MCI_SEEK,MCI_SEEK_TO_START,0);
mciSendCommand(wDeviceID, MCI_PLAY, MCI_NOTIFY,
(DWORD)(LPVOID) &mciPlayParms);
return 0;
}
return 0;
case WM_SOCKET:
wEvent = WSAGETSELECTEVENT (lParam);
wError = WSAGETSELECTERROR (lParam);
switch (wEvent)
{
case FD_CONNECT:
//ferme la boite client quand connecté
hDlg=GetWindow(hwnd,GW_HWNDPREV);
EndDialog(hDlg,0);
MessageBox(hwnd,"Vous etes connecté !","Connexion",MB_OK|MB_ICONINFORMATION);
//hMenu=GetMenu(hwnd);
EnableMenuItem(hMenu,IDM_MESS,MF_ENABLED);
DrawMenuBar(hwnd);//pour rafraichir menu
reso=1;//commencement jeu réso
messageZ(hwnd,"Sélectionnez un bateau dans la grille avec la souris.");
InvalidateRect(hwnd, NULL, TRUE);
UpdateWindow(hwnd);
return 0;
case FD_ACCEPT:
if(acc==1)return 0;
iLen=sizeof(addr_accept);
Sinv=accept(Sser,(SOCKADDR*)&addr_accept,&iLen);
if(Sinv==INVALID_SOCKET){ShoErr("accept",WSAGetLastError());return 0;}//warzaza
if(SOCKET_ERROR==WSAAsyncSelect(Sinv,hwnd,WM_SOCKET,FD_READ|FD_WRITE|FD_CLOSE))
{ShoErr("WSAAsyncSeletc \n(pour la 2eme socket serveur)",WSAGetLastError());return 0;}
acc=1;
//ferme la boite serveur quand connecté
hDlg=GetWindow(hwnd,GW_HWNDPREV);
EndDialog(hDlg,0);
strcpy(adre,inet_ntoa(addr_accept.sin_addr));
ad=inet_addr(adre);
invite=gethostbyaddr((char *)&ad,4,AF_INET); //,strlen(adre),PF_INET);
if(invite!=NULL){
strcat(adre,"\n");
strcat(adre,invite->h_name);}
strcat(adre," vient de se connecter");
MessageBox(hwnd,adre,"connexion",MB_OK|MB_ICONINFORMATION);
hMenu=GetMenu(hwnd);
EnableMenuItem(hMenu,IDM_MESS,MF_ENABLED);
DrawMenuBar(hwnd);//pour rafraichir menu
reso=1;//commencement jeu réso
messageZ(hwnd,"Sélectionnez un bateau dans la grille avec la souris.");
InvalidateRect(hwnd, NULL, TRUE);
UpdateWindow(hwnd);
return 0;
case FD_CLOSE:
MessageBox(hwnd,"l'autre s'est déconnecté (FD_close)","connexion",0);
reinit();cleanNet();
InvalidateRect(hwnd,NULL,TRUE);
UpdateWindow(hwnd);
//affiche(hwnd,"L'autre s'est déconnecté");
return 0;
case FD_READ:
if(serv==1)
{
iReadLen=recv(Sinv,(char*)&readbyte,sizeof(readbyte),0);
if(iReadLen==0){ShoErr("recv",WSAGetLastError());return 0;}//warzaza
for(i=0;i<150;i++){in[i]=readbyte[i];}
Angriff(hwnd);
//messageZ(hwnd,readbyte);
return 0;
}
if(cli==1)
{
iReadLen=recv(Scli,(char*)&readbyte,sizeof(readbyte),0);
if(iReadLen==0){ShoErr("recv",WSAGetLastError());return 0;}//warzaza
for(i=0;i<150;i++){in[i]=readbyte[i];}
Angriff(hwnd);
//messageZ(hwnd,readbyte);
return 0;
}
case FD_WRITE:
//strcpy(buffer,MessRez);
//iSendLen=send(Sinv,buffer,sizeof(buffer),0);
return 0;
break;
}
return 0;
case WM_MOUSEMOVE:
Xm=LOWORD(lParam);
Ym=HIWORD(lParam);
/*
X=( (Xm-50)- ( (Xm-50)%20 ) )/20;
Y=( (Ym-50)- ( (Ym-50)%20 ) )/20;
//XD et YD pour la grille de droite
XD=( (Xm-350)- ( (Xm-350)%20 ) )/20;
YD=( (Ym-50)- ( (Ym-50)%20 ) )/20;*/
return 0;
case WM_LBUTTONDOWN:
X=( (Xm-50)- ( (Xm-50)%20 ) )/20;
Y=( (Ym-50)- ( (Ym-50)%20 ) )/20;
//XD et YD pour la grille de droite
XD=( (Xm-350)- ( (Xm-350)%20 ) )/20;
YD=( (Ym-50)- ( (Ym-50)%20 ) )/20;
if((finR==1)||(finS==1)){for(j=0;j<10;j++)for(i=0;i<10;i++){monpay3[j][i]=0;}if(sourFlag==1){sourFlag=0;InvalidateRect(hwnd,NULL,TRUE);UpdateWindow(hwnd);return 0;}if(sourFlag==0){sourFlag=1;InvalidateRect(hwnd,NULL,TRUE);UpdateWindow(hwnd);return 0;}}
if((reso==1)||((solo==1)&&(combatS==0)))
{
//zone de sélection des bato
if((Xm>50)&&(Xm<250)&&(Ym>290)&&(Ym<330)&&(fix==0))
{
messageZ(hwnd,"Dans la grille de gauche, choisissez le sens du bouton gauche, puis fixez du bouton droit");
if((Xm>50)&&(Xm<150)&&(Ym>290)&&(Ym<310)&&(choiB1==0)) {choiB1=1;zero(&choiB5, &choiB7, &choiB3, &choiB11, &choiB9);freshB(hwnd);return 0;}
if((Xm>150)&&(Xm<210)&&(Ym>290)&&(Ym<310)&&(choiB5==0)) {choiB5=1;zero(&choiB1, &choiB7, &choiB3, &choiB11, &choiB9);freshB(hwnd);return 0;}
if((Xm>210)&&(Xm<250)&&(Ym>290)&&(Ym<310)&&(choiB7==0)) {choiB7=1;zero(&choiB1, &choiB5,& choiB3, &choiB11, &choiB9);freshB(hwnd);return 0;}
if((Xm>50)&&(Xm<130)&&(Ym>310)&&(Ym<330)&&(choiB3==0)) {choiB3=1;zero(&choiB1, &choiB5, &choiB7, &choiB11, &choiB9);freshB(hwnd);return 0;}
if((Xm>130)&&(Xm<190)&&(Ym>310)&&(Ym<330)&&(choiB11==0)) {choiB11=1;zero(&choiB1, &choiB5, &choiB7, &choiB3, &choiB9);freshB(hwnd);return 0;}
if((Xm>190)&&(Xm<250)&&(Ym>310)&&(Ym<330)&&(choiB9==0)) {choiB9=1;zero(&choiB1, &choiB5, &choiB7, &choiB3, &choiB11);freshB(hwnd);return 0;}
}
if((Xm>50)&&(Xm<250)&&(Ym>290)&&(Ym<330)&&(fix==1))MessageBox(hwnd,"Vous devez fixer le bateau\ndu bouton droit de la souris.","Mais non !",MB_OK|MB_ICONSTOP);
//zone grille perso pour placement des bato
if((Xm>50)&&(Xm<250)&&(Ym>50)&&(Ym<250))
{
/*zero bato erreur*/for(i=0;i<10;i++)for(j=0;j<10;j++)monpay3[i][j]=0;
errscan=1;//doit etre ramené à 0 si scan bon, pour éviter fixer bateau inexistant
if((choiB1==1)&&(choiSens==0)&&((monpay[Y][X]==0)||(monpay[Y][X]==3))&&(scanCorp(5,3,2,hwnd)==1)){purgtab(3);choiB1=1;freshB(hwnd);monpay[Y][X]=2;Acc=2;freshGrilG(hwnd);fix=1;}
if((choiB1==1)&&(choiSens==1)&&((monpay[Y][X]==0)||(monpay[Y][X]==2))&&(scanCorp(5,2,3,hwnd)==1)){purgtab(2);choiB1=1;freshB(hwnd);monpay[Y][X]=3;Acc=3;freshGrilG(hwnd);fix=1;}
if((choiB5==1)&&(choiSens==0)&&((monpay[Y][X]==0)||(monpay[Y][X]==7))&&(scanCorp(3,7,6,hwnd)==1)){purgtab(7);choiB5=1;freshB(hwnd);monpay[Y][X]=6;Acc=6;freshGrilG(hwnd);fix=1;}
if((choiB5==1)&&(choiSens==1)&&((monpay[Y][X]==0)||(monpay[Y][X]==6))&&(scanCorp(3,6,7,hwnd)==1)){purgtab(6);choiB5=1;freshB(hwnd);monpay[Y][X]=7;Acc=7;freshGrilG(hwnd);fix=1;}
if((choiB7==1)&&(choiSens==0)&&((monpay[Y][X]==0)||(monpay[Y][X]==9))&&(scanCorp(2,9,8,hwnd)==1)){purgtab(9);choiB7=1;freshB(hwnd);monpay[Y][X]=8;Acc=8;freshGrilG(hwnd);fix=1;}
if((choiB7==1)&&(choiSens==1)&&((monpay[Y][X]==0)||(monpay[Y][X]==8))&&(scanCorp(2,8,9,hwnd)==1)){purgtab(8);choiB7=1;freshB(hwnd);monpay[Y][X]=9;Acc=9;freshGrilG(hwnd);fix=1;}
if((choiB3==1)&&(choiSens==0)&&((monpay[Y][X]==0)||(monpay[Y][X]==5))&&(scanCorp(4,5,4,hwnd)==1)){purgtab(5);choiB3=1;freshB(hwnd);monpay[Y][X]=4;Acc=4;freshGrilG(hwnd);fix=1;}
if((choiB3==1)&&(choiSens==1)&&((monpay[Y][X]==0)||(monpay[Y][X]==4))&&(scanCorp(4,4,5,hwnd)==1)){purgtab(4);choiB3=1;freshB(hwnd);monpay[Y][X]=5;Acc=5;freshGrilG(hwnd);fix=1;}
if((choiB11==1)&&(choiSens==0)&&((monpay[Y][X]==0)||(monpay[Y][X]==13))&&(scanCorp(3,13,12,hwnd)==1)){purgtab(13);choiB11=1;freshB(hwnd);monpay[Y][X]=12;Acc=12;freshGrilG(hwnd);fix=1;}
if((choiB11==1)&&(choiSens==1)&&((monpay[Y][X]==0)||(monpay[Y][X]==12))&&(scanCorp(3,12,13,hwnd)==1)){purgtab(12);choiB11=1;freshB(hwnd);monpay[Y][X]=13;Acc=13;freshGrilG(hwnd);fix=1;}
if((choiB9==1)&&(choiSens==0)&&((monpay[Y][X]==0)||(monpay[Y][X]==11))&&(scanCorp(3,11,10,hwnd)==1)){purgtab(11);choiB9=1;freshB(hwnd);monpay[Y][X]=10;Acc=10;freshGrilG(hwnd);fix=1;}
if((choiB9==1)&&(choiSens==1)&&((monpay[Y][X]==0)||(monpay[Y][X]==10))&&(scanCorp(3,10,11,hwnd)==1)){purgtab(10);choiB9=1;freshB(hwnd);monpay[Y][X]=11;Acc=11;freshGrilG(hwnd);fix=1;}
if(choiSens==0)choiSens=1;else choiSens=0;
}
}//if((solo==1)&&(combat==0))
if((solo==1)&&(combatS==1)&&(finS==0)&&(Xm>350)&&(Xm<550)&&(Ym>50)&&(Ym<250))
{/*if((music==1)&&(flagson==0)){flagson=1;playMIDIFile(hwnd, "song.mid");}*/
//if((music==1)&&(flagson==0)){flagson=1;SendMessage(hwnd,WM_COMMAND,ID_MIDI,0);}
if(ennemi2[YD][XD]>0)messageZ(hwnd,"MAIS NON !!! Déjà fait...");
else combatsolo(hwnd,XD,YD); }
if((reso==1)&&(combatR==1)&&(finR==0)&&(montour==1)&&(Xm>350)&&(Xm<550)&&(Ym>50)&&(Ym<250))
{
/*if((music==1)&&(flagson==0)){playMIDIFile(hwnd, "song.mid");}*/
//if((music==1)&&(flagson==0)){flagson=1;SendMessage(hwnd,WM_COMMAND,ID_MIDI,0);}
if(ennemi2[YD][XD]>0){messageZ(hwnd,"MAIS NON !!! Déjà fait...");return 0;}
montour=0;
freshTorp(hwnd);
for(i=0;i<150;i++)buffer[i]=0;
buffer[120]=1;
buffer[121]=XD;
buffer[122]=YD;
if(serv==1){iSendLen=send(Sinv,buffer,sizeof(buffer),0);if(iSendLen==0){ShoErr("send",WSAGetLastError());return 0;}}
if(cli==1){iSendLen=send(Scli,buffer,sizeof(buffer),0);if(iSendLen==0){ShoErr("send",WSAGetLastError());return 0;}}
}
return 0;
case WM_RBUTTONDOWN:
if(((reso==1)&&(combatR==0))||((solo==1)&&(combatS==0)))
{
if((Xm>50)&&(Xm<250)&&(Ym>50)&&(Ym<250)&&(fix==1)&&(errscan==0))
{
if(choiB1==1){choiB1=2;freshGrilG(hwnd);freshB(hwnd);construct(5);}
if(choiB5==1){choiB5=2;freshGrilG(hwnd);freshB(hwnd);construct(3);}
if(choiB7==1){choiB7=2;freshGrilG(hwnd);freshB(hwnd);construct(2);}
if(choiB3==1){choiB3=2;freshGrilG(hwnd);freshB(hwnd);construct(4);}
if(choiB11==1){choiB11=2;freshGrilG(hwnd);freshB(hwnd);construct(3);}
if(choiB9==1){choiB9=2;freshGrilG(hwnd);freshB(hwnd);construct(3);}
fix=0;
}
if((reso==0)&&(choiB1==2)&&(choiB5==2)&&(choiB7==2)&&(choiB3==2)&&(choiB11==2)&&(choiB9==2))
{
putenemi(5);putenemi(3);putenemi(2);putenemi(4);putenemi(3);putenemi(3);//placement des ato sur terrain ennemi
freshGrilD(hwnd);
combatS=1;
//initialisation des tablo de combat
messageZ(hwnd,"Tout vos bateaux sont placés, le jeu va pouvoir commencer.");
}//solo
if((reso==1)&&(choiB1==2)&&(choiB5==2)&&(choiB7==2)&&(choiB3==2)&&(choiB11==2)&&(choiB9==2))
{
moiready=1;//tous mes bateaux sont placés
for(i=0;i<150;i++)buffer[i]=0;
buffer[110]=1;
if(serv==1){iSendLen=send(Sinv,buffer,sizeof(buffer),0);if(iSendLen==0){ShoErr("send",WSAGetLastError());return 0;}}//warzaza
if(cli==1){iSendLen=send(Scli,buffer,sizeof(buffer),0);if(iSendLen==0){ShoErr("send",WSAGetLastError());return 0;}}
combatR=1;
//messageZ(hwnd,"Tout vos bateaux sont placés. Attente de l'adversaire");
if((moiready==1)&&(luiready==1)&&(serv==1))choiprem(hwnd);
}//reso
} //if((solo==1)&&(combatS==0))
return 0;
case WM_DESTROY:
DeleteDC(hdcMem1);
DeleteDC(hdcMem2);
DeleteDC(hdcMem3);
DeleteDC(hdcMem4);
DeleteDC(hdcMem5);
DeleteDC(hdcMem6);
DeleteDC(hdcMem7);
DeleteDC(hdcMem8);
DeleteDC(hdcMem9);
DeleteDC(hdcMem10);
DeleteDC(hdcMem11);
DeleteDC(hdcMem12);
//essai affichage victoire
DeleteDC(hdcVic);
//fin réseau
cleanNet();
PostQuitMessage(0);
return 0;
break;
}//fin switch
return DefWindowProc(hwnd,message,wParam,lParam);
}//fin wndproc
// -------------------------------------------------------------------------
//
//
// BOITES DE DIALOG
//
//
// --------------------------------------------------------------------------
BOOL CALLBACK AboutDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
return TRUE;
case WM_COMMAND:
switch (LOWORD (wParam))
{
case IDOK:
//case IDCANCEL:
EndDialog(hDlg,0);
return TRUE;
}
break;
}
return FALSE;
}//DlgProc
BOOL CALLBACK MessDlgProc(HWND hDlg,UINT message,WPARAM wParam,LPARAM lParam)
{
int i;
char buf[10];
switch (message)
{
case WM_INITDIALOG:
return TRUE;
case WM_COMMAND:
switch (LOWORD (wParam))
{
case IDOK:
for(i=0;i<150;i++)buffer[i]=0;
GetDlgItemText(hDlg,IDC_MESS,MessRez,100);
strcpy(buffer,MessRez);
//allumage du drapeau message
buffer[140]=1;
if(serv==1){iSendLen=send(Sinv,buffer,sizeof(buffer),0);if(iSendLen==0){ShoErr("send",WSAGetLastError());return 0;}}//warzaza
if(cli==1){iSendLen=send(Scli,buffer,sizeof(buffer),0);if(iSendLen==0){ShoErr("send",WSAGetLastError());return 0;}}
EndDialog(hDlg,0);
return TRUE;
case IDCANCEL:
EndDialog(hDlg,0);
return TRUE;
}
GetDlgItemText(hDlg,IDC_MESS,MessRez,100);
wsprintf(buf,"%d",100-strlen(MessRez));
SetDlgItemText(hDlg,IDC_CHIFFRE,buf);
break;
}
return FALSE;
}//messDlgproc
BOOL CALLBACK ClieDlgProc(HWND hDlg,UINT message,WPARAM wParam,LPARAM lParam)
{
HWND hwnd;
switch (message)
{
case WM_INITDIALOG:
idok=0;
SendDlgItemMessage(hDlg,IDOK,WM_COMMAND,0,BN_DISABLE);
return TRUE;
case WM_COMMAND:
switch (LOWORD (wParam))
{
case IDOK:
if(idok==0)
{
idok=1;
if(soc==0)initsock();
GetDlgItemText(hDlg,IDC_EDIT_IP,szIPAddr,64);
if(liAddr()==FALSE)
{MessageBox(hDlg,"Adresse mal entrée. Recommencez.","Erreur !",MB_OK|MB_ICONSTOP);
idok=0;
return TRUE;}
hwnd=GetParent(hDlg);
if(client(hDlg,hwnd)==FALSE){reinit();cleanNet();EndDialog(hDlg,0);}
}
return TRUE;
case IDCANCEL:
if(IDNO==MessageBox(hDlg,"Cette fonction stoppera le client,\net toute connexion que vous aurez entamée.\nEtes vous sûr ?","oula!!!!!!",MB_YESNO|MB_ICONQUESTION))return TRUE;
cleanNet();
EndDialog(hDlg,0);
return TRUE;
}
break;
}
return FALSE;
}//ClieDlgproc
BOOL CALLBACK ServDlgProc(HWND hDlg,UINT message,WPARAM wParam,LPARAM lParam)
{
HWND hwnd;
switch (message)
{
case WM_INITDIALOG:
hwnd=GetParent(hDlg);
if(soc==0)initsock();
if(serveur(hDlg,hwnd)==FALSE){reinit();cleanNet();EndDialog(hDlg,0);}
return TRUE;
case WM_COMMAND:
switch (LOWORD (wParam))
{
case IDCANCEL:
if(IDNO==MessageBox(hDlg,"Cette fonction stoppera le serveur.\nEtes vous sûr ?","oula!!!!!!",MB_YESNO|MB_ICONQUESTION))return TRUE;
cleanNet();
EndDialog(hDlg,0);
return TRUE;
case IDC_FSERV:
EndDialog(hDlg,0);
return TRUE;
}
break;
}
return FALSE;
}//ServDlgproc
// -------------------------------------------------------------------------
//
//
// SS FONTIONS FENETRE PRINCIPALE
//
//
// --------------------------------------------------------------------------
void messageZ(HWND hwnd,LPSTR phrase)
{
RECT Mrct;
int longu, debut,i;
//réinitialisation de essai
for(i=0;i<199;i++)essai[i]=32;
strcpy(string1," ");
strcpy(string2," ");
strcpy(string3," ");
strcpy(string4," ");
strcpy(essai,phrase);
//calcul de la longueur de la zone de texte
Longueur=200/cxChar;
k=0;
debut=0;
longu=Longueur;
//if(longu>34)longu=34;
if(longu<=(int)strlen(essai)){while(essai[longu]!=32)longu--;}
if(longu<=debut)longu=debut+Longueur;
for(i=0;i<longu;i++){string1[k]=essai[i];k++;}k=0;
debut=longu;
longu=longu+Longueur;
if(longu<=(int)strlen(essai)){while(essai[longu]!=32)longu--;}
if(longu<=debut)longu=debut+Longueur;
for(i=debut;i<longu;i++){string2[k]=essai[i];k++;}k=0;
debut=longu;
longu=longu+Longueur;
if(longu<=(int)strlen(essai)){while(essai[longu]!=32)longu--;}
if(longu<=debut)longu=debut+Longueur;
for(i=debut;i<longu;i++){string3[k]=essai[i];k++;}k=0;
debut=longu;
longu=longu+Longueur;
if(longu<=(int)strlen(essai)){while(essai[longu]!=32)longu--;}
if(longu<=debut)longu=debut+Longueur;
for(i=debut;i<longu;i++){string4[k]=essai[i];k++;}k=0;
Mrct.top=275;
Mrct.bottom=350;
Mrct.left=350;
Mrct.right=550;
InvalidateRect(hwnd,&Mrct,TRUE);
UpdateWindow(hwnd);
return;
}
void zero(int* a,int* b,int* c,int* d,int* e )
{
if(*a<2)*a=0;
if(*b<2)*b=0;
if(*c<2)*c=0;
if(*d<2)*d=0;
if(*e<2)*e=0;
return;
}
void purgtab(int a)
{
int i,j;
for(i=0;i<10;i++)
for(j=0;j<10;j++)
{
if(monpay[j][i]==a)monpay[j][i]=0;
}//fin du for
}
void freshB( HWND hwnd)
{
RECT rct;
//rafraichisssement zone selection bato
rct.top=290;
rct.left=50;
rct.right=250;
rct.bottom=330;
InvalidateRect(hwnd,&rct,TRUE);
UpdateWindow(hwnd);
return;
}
void freshGrilG( HWND hwnd)
{
RECT rct;
//rafraichisssement grille gauche
rct.top=50;
rct.left=50;
rct.right=250;
rct.bottom=250;
InvalidateRect(hwnd,&rct,TRUE);
UpdateWindow(hwnd);
return;
}
void freshGrilD( HWND hwnd)
{
RECT rct;
//rafraichisssement grille droite
rct.top=50;
rct.left=350;
rct.right=550;
rct.bottom=250;
InvalidateRect(hwnd,&rct,TRUE);
UpdateWindow(hwnd);
return;
}
void freshCaseG(HWND hwnd,int i,int j)
{
RECT rct;
rct.top=50+20*j;
rct.left=50+20*i;
rct.right=50+20*i+20;
rct.bottom=50+20*j+20;
InvalidateRect(hwnd,&rct,TRUE);
UpdateWindow(hwnd);
}
void freshCaseD(HWND hwnd,int i,int j)
{
RECT rct;
rct.top=50+20*j;
rct.left=350+20*i;
rct.right=350+20*i+20;
rct.bottom=50+20*j+20;
InvalidateRect(hwnd,&rct,TRUE);
UpdateWindow(hwnd);
}
void freshTorp(HWND hwnd)
{
RECT rct;
rct.top=20;
rct.left=290;
rct.right=310;
rct.bottom=40;
InvalidateRect(hwnd,&rct,TRUE);
UpdateWindow(hwnd);
}
int scanCorp(int nbcorp,int a,int b,HWND hwnd)//nb de cases à inspecter, et numéro à purger
{
int x=X;
int y=Y;
int i;
if (choiSens==0)//horizontal
{
//regarde le point derriere
if((x>0)&&(monpay[y][x-1]!=0)) {purgtab(a);freshGrilG(hwnd);messageZ(hwnd,"Trop près du bateau de gauche. Il faut au moins une case d'écart.");monpay3[Y][X]=b;freshGrilG(hwnd);return 0;}
//fer à cheval horz
for(i=0;i<nbcorp;i++)
{
if((y>0)&&(monpay[y-1][x]!=0)) {purgtab(a);freshGrilG(hwnd);messageZ(hwnd,"Trop près du bateau qui est au dessus. Il faut au moins une case d'écart.");monpay3[Y][X]=b;freshGrilG(hwnd);return 0;}
if((y<9)&&(monpay[y+1][x]!=0)) {purgtab(a);freshGrilG(hwnd);messageZ(hwnd,"Trop près du bateau qui est en dessous. Cliquez encore un fois pour essayer verticalement.");monpay3[Y][X]=b;freshGrilG(hwnd);return 0;}
if((x<9)&&(monpay[y][x+1]!=0)) {purgtab(a);freshGrilG(hwnd);messageZ(hwnd,"Trop près du bateau de droite. Cliquez encore un fois pour essayer verticalement.");monpay3[Y][X]=b;freshGrilG(hwnd);return 0;}
if((x==9)&&(i<nbcorp-1)){purgtab(a);freshGrilG(hwnd);messageZ(hwnd,"Trop à droite. Cliquez encore un fois pour essayer verticalement.");monpay3[Y][X]=b;freshGrilG(hwnd);return 0;}
x++;
}//for
// si tout est bien, retourne 1;
freshGrilG(hwnd);
errscan=0;
messageZ(hwnd,"Fixer le bateau du bouton droit de la souris");
return 1;
}//if choisens=0
if (choiSens==1)//vertical
{
//monpay[Y][X]==0
//regarde le point derriere
if((y>0)&&(monpay[y-1][x]!=0)) {purgtab(a);freshGrilG(hwnd);messageZ(hwnd,"Trop près du bateau qui est au dessus. Il faut au moins une case d'écart.");monpay3[Y][X]=b;freshGrilG(hwnd);return 0;}
//fer à cheval horz
for(i=0;i<nbcorp;i++)
{
if((x>0)&&(monpay[y][x-1]!=0)) {purgtab(a);freshGrilG(hwnd);messageZ(hwnd,"Trop près du bateau de gauche. Il faut au moins une case d'écart.");monpay3[Y][X]=b;freshGrilG(hwnd);return 0;}
if((x<9)&&(monpay[y][x+1]!=0)) {purgtab(a);freshGrilG(hwnd);messageZ(hwnd,"Trop près du bateau de droite. Cliquez encore un fois pour essayer horizontalement.");monpay3[Y][X]=b;freshGrilG(hwnd);return 0;}
if((y<9)&&(monpay[y+1][x]!=0)) {purgtab(a);freshGrilG(hwnd);messageZ(hwnd,"Trop près du bateau qui est en dessous. Cliquez encore un fois pour essayer horizontalement.");monpay3[Y][X]=b;freshGrilG(hwnd);return 0;}
if((y==9)&&(i<nbcorp-1)){purgtab(a);freshGrilG(hwnd);messageZ(hwnd,"Trop bas. Cliquez encore un fois pour essayer horizontalement.");monpay3[Y][X]=b;freshGrilG(hwnd);return 0;}
y++;
}//for
// si tout est bien, retourne 1;
freshGrilG(hwnd);
errscan=0;
messageZ(hwnd,"Fixer le bateau du bouton droit de la souris");
return 1;
}//if choisens=1
return 0;//pb si arrivéjusque là
}
void construct(int nbcorp)
{
int i;
int j;
int k;
for(i=0;i<10;i++)
for(j=0;j<10;j++)
if(monpay[j][i]==Acc)
{
for(k=1;k<nbcorp;k++)
{
if(Acc%2==0)//les bato pair sont horizontaux
{
monpay[j][i+1]=1;//saute la tete qui a déjà une valeur
i++;
}
if(Acc%2==1)//les bato impair sont verticaux
{
monpay[j+1][i]=1;//saute la tete qui a déjà une valeur
j++;
}
}//for
}
return;
}
void putenemi(int nbcorp)
{
//choisi tete
//init rand
srand((unsigned)time(NULL));
do
{
x=(rand()%10);
y=(rand()%10);
//choisi verti ou hor au hasard
vert=(rand()%2);
}
while((ennemi[y][x]!=0)||(scanEnemi( nbcorp)==0));
ennemi[y][x]=1;
constrEnemi(nbcorp);
return;
}
int scanEnemi( int nbcorp)
{
int i;
int a,b;
a=x;
b=y;
if (vert==0)//horizontal
{
//regarde le point derriere
if((a>0)&&(ennemi[b][a-1]!=0)) {return 0;}
//fer à cheval horz
for(i=0;i<nbcorp;i++)
{
if((b>0)&&(ennemi[b-1][a]!=0)) {return 0;}
if((b<9)&&(ennemi[b+1][a]!=0)) {return 0;}
if((a<9)&&(ennemi[b][a+1]!=0)) {return 0;}
if((a==9)&&(i<nbcorp-1)){return 0;}
a++;
}//for
// si tout est bien, retourne 1;
return 1;
}//if vert==0
if (vert==1)//vertical
{
//regarde le point derriere
if((b>0)&&(ennemi[b-1][a]!=0)) {return 0;}
//fer à cheval horz
for(i=0;i<nbcorp;i++)
{
if((a>0)&&(ennemi[b][a-1]!=0)) {return 0;}
if((a<9)&&(ennemi[b][a+1]!=0)) {return 0;}
if((b<9)&&(ennemi[b+1][a]!=0)) {return 0;}
if((b==9)&&(i<nbcorp-1)){return 0;}
b++;
}//for
// si tout est bien, retourne 1;
return 1;
}//if vert==0
return 0;//si arrivé là,pb.
}
void constrEnemi(int nbcorp)
{
int a;
int b;
int k;
a=x;
b=y;
for(k=1;k<nbcorp;k++)
{
if(vert==0)//horizontaux
{
ennemi[b][a+1]=1;//saute la tete qui a déjà une valeur
a++;
}
if(vert==1)//lverticaux
{
ennemi[b+1][a]=1;//saute la tete qui a déjà une valeur
b++;
}
}//for
return;
}
void combatsolo(HWND hwnd,int a,int b)
{
int i,j;
//si on fait mouche, le 2eme tableau indique une valeur de 2, donc une croix.sinon 1(rond)
if(tour==0) //joueur
{
if(ennemi[b][a]>0){ennemi2[b][a]=2;scoreJ++;}//mouche
else ennemi2[b][a]=1;//rond blanc
freshCaseD(hwnd,a,b);
//son
if((scoreJ>scoreJan)&&(son==1))PlaySound("EXPLO.WAV",NULL,SND_RESOURCE |SND_ASYNC );
if((scoreJ==scoreJan)&&(son==1))PlaySound("PLOUF.WAV",NULL,SND_RESOURCE |SND_ASYNC );
scoreJan=scoreJ;
if(scoreJ==20)
{
finS=1;
messageZ(hwnd,"Vous avez gagné");
//essai affichage de fin
aff=1;
InvalidateRect(hwnd,NULL,TRUE);
UpdateWindow(hwnd);
return;
}
tour=1;
freshTorp(hwnd);
}
if(tour==1) //ordinateur
{
//fonction pour ne pas tirer dans une case encadrée de case déjà tirée
/*
for(j=0;j<10;j++)
for(i=0;i<10;i++){
if((monpay2[j][i]==0)&&((monpay2[j-1][i]>0)||(j==0))&&((monpay2[j][i+1]>0)||(i==9))&&((monpay2[j+1][i]>0)||(j==9))&&((monpay2[j][i-1]>0)||(i==0))&&((monpay2[j-1][i]<3)||(j==0))&&((monpay2[j][i+1]<3)||(i==9))&&((monpay2[j+1][i]<3)||(j==9))&&((monpay2[j][i-1]<3)||(i==0)))monpay2[i][j]=3;
}*/
if(mouche==2)
{
if(passe==1)
{
//élimination cases déjà cochées ou bord
if((sens==1)&&((deux[1]==0)||(monpay2[(deux[1]-1)][(deux[0])]>0)))passe=3;
if((sens==1)&&((deux[1]!=0)&&(monpay2[(deux[1]-1)][(deux[0])]==0)))passe=2;
if((sens==1)&&((deux[1]!=0)&&(monpay2[(deux[1]-1)][(deux[0])]==0)&&(optimiz()==TRUE))){mouche=0;passe=0;tues[t]=tuage;t++;}
if((sens==2)&&((deux[0]==9)||(monpay2[(deux[1])][(deux[0]+1)]>0)))passe=3;
if((sens==2)&&((deux[0]!=9)&&(monpay2[(deux[1])][(deux[0]+1)]==0)))passe=2;
if((sens==2)&&((deux[0]!=9)&&(monpay2[(deux[1])][(deux[0]+1)]==0)&&(optimiz()==TRUE))){mouche=0;passe=0;tues[t]=tuage;t++;}
if((sens==3)&&((deux[1]==9)||(monpay2[(deux[1]+1)][(deux[0])]>0)))passe=3;
if((sens==3)&&((deux[1]!=9)&&(monpay2[(deux[1]+1)][(deux[0])]==0)))passe=2;
if((sens==3)&&((deux[1]!=9)&&(monpay2[(deux[1]+1)][(deux[0])]==0)&&(optimiz()==TRUE))){mouche=0;passe=0;tues[t]=tuage;t++;}
if((sens==4)&&((deux[0]==0)||(monpay2[(deux[1])][(deux[0]-1)]>0)))passe=3;
if((sens==4)&&((deux[0]!=0)&&(monpay2[(deux[1])][(deux[0]-1)]==0)))passe=2;
if((sens==4)&&((deux[0]!=0)&&(monpay2[(deux[1])][(deux[0]-1)]==0)&&(optimiz()==TRUE))){mouche=0;passe=0;tues[t]=tuage;t++;}
}
if(passe==2)
{
//on continue dans le sens
if(sens==1){i=deux[0]; j=deux[1]-1;}
if(sens==2){i=deux[0]+1; j=deux[1]; }
if(sens==3){i=deux[0]; j=deux[1]+1;}
if(sens==4){i=deux[0]-1; j=deux[1]; }
if (monpay[j][i]==0){monpay2[j][i]=1; passe=3; tour=0;freshTorp(hwnd); freshCaseG(hwnd,i,j); return;} //return pour ne pas aller dans passe2
if (monpay[j][i]>0) {monpay2[j][i]=2; deux[0]=i; deux[1]=j; passe=1; tuage++;scoreO++;bords2();}
}//passe2
if(passe==3)
{
//demi tour
//
deux[0]=premier[0]; deux[1]=premier[1];
passe=4;
}
if(passe==4)
{
//élimination cases déjà cochées ou bord
if((sens==1)&&((deux[1]==9)||(monpay2[(deux[1]+1)][(deux[0])]>0))){mouche=0;passe=0;}
if((sens==1)&&((deux[1]!=9)&&(monpay2[(deux[1]+1)][(deux[0])]==0))){passe=5;}
if((sens==1)&&((deux[1]!=9)&&(monpay2[(deux[1]+1)][(deux[0])]==0)&&(optimiz()==TRUE))){mouche=0;passe=0;tues[t]=tuage;t++;}
if((sens==2)&&((deux[0]==0)||(monpay2[(deux[1])][(deux[0]-1)]>0))){mouche=0;passe=0;}
if((sens==2)&&((deux[0]!=0)&&(monpay2[(deux[1])][(deux[0]-1)]==0))){passe=5;}
if((sens==2)&&((deux[0]!=0)&&(monpay2[(deux[1])][(deux[0]-1)]==0)&&(optimiz()==TRUE))){mouche=0;passe=0;tues[t]=tuage;t++;}
if((sens==3)&&((deux[1]==0)||(monpay2[(deux[1]-1)][(deux[0])]>0))){mouche=0;passe=0;}
if((sens==3)&&((deux[1]!=0)&&(monpay2[(deux[1]-1)][(deux[0])]==0))){passe=5;}
if((sens==3)&&((deux[1]!=0)&&(monpay2[(deux[1]-1)][(deux[0])]==0)&&(optimiz()==TRUE))){mouche=0;passe=0;tues[t]=tuage;t++;}
if((sens==4)&&((deux[0]==9)||(monpay2[(deux[1])][(deux[0]+1)]>0))){mouche=0;passe=0;}
if((sens==4)&&((deux[0]!=9)&&(monpay2[(deux[1])][(deux[0]+1)]==0))){passe=5;}
if((sens==4)&&((deux[0]!=9)&&(monpay2[(deux[1])][(deux[0]+1)]==0)&&(optimiz()==TRUE))){mouche=0;passe=0;tues[t]=tuage;t++;}
}
if(passe==5) //on considere le sens comme son contraire
{
if(sens==1){i=deux[0];j=deux[1]+1;}
if(sens==2){i=deux[0]-1;j=deux[1];}
if(sens==3){i=deux[0];j=deux[1]-1;}
if(sens==4){i=deux[0]+1;j=deux[1];}
if (monpay[j][i]==0) {monpay2[j][i]=1;mouche=0;tour=0;freshTorp(hwnd);passe=0;tues[t]=tuage;t++;freshCaseG(hwnd,i,j);return;} //return pour ne pas aller dans passe2
if (monpay[j][i]>0) {monpay2[j][i]=2;deux[0]=i;deux[1]=j;passe=4;tuage++;scoreO++;bords2();}
}//passe5
}//mouche 2
if(mouche==1)
{
//ronde
//ronde aléatoire // 1
srand((unsigned)time(NULL)); // 4 2
// 3
//stockage des données interdite, qu'elle aient été déjà
//employées ou par le positionnement de l'impact initial
//valeur dues à la position
if(histo==0){
for(r=0;r<4;r++)rondeInterd[r]=0;//réinitialisation
r=0;
if(premier[0]==9){rondeInterd[r]=2;r++;}
if(premier[0]==0){rondeInterd[r]=4;r++;}
if(premier[1]==9){rondeInterd[r]=3;r++;}
if(premier[1]==0){rondeInterd[r]=1;r++;}
//elimination case déjà cochées
if((premier[1]>0)&&(monpay2[(premier[1]-1)][(premier[0])]>0)){rondeInterd[r]=1;r++;}
if((premier[0]<9)&&(monpay2[(premier[1])][(premier[0]+1)]>0)){rondeInterd[r]=2;r++;}
if((premier[1]<9)&&(monpay2[(premier[1]+1)][(premier[0])]>0)){rondeInterd[r]=3;r++;}
if((premier[0]>0)&&(monpay2[(premier[1])][(premier[0]-1)]>0)){rondeInterd[r]=4;r++;}
histo=1;
}
do ronde=(rand()%5);
while((ronde==rondeInterd[0])||(ronde==rondeInterd[1])||(ronde==rondeInterd[2])||(ronde==rondeInterd[3]));
switch(ronde){
case 1:i=premier[0];j=premier[1]-1;
if(monpay[j][i]==0){monpay2[j][i]=1;rondeInterd[r]=1;r++;}
if(monpay[j][i]>0){monpay2[j][i]=2;deux[0]=i;deux[1]=j;sens=1;mouche=2;passe=1;tuage++;scoreO++;bords();}
break;
case 2:i=premier[0]+1;j=premier[1];
if(monpay[j][i]==0){monpay2[j][i]=1;rondeInterd[r]=2;r++;}
if(monpay[j][i]>0){monpay2[j][i]=2;deux[0]=i;deux[1]=j;sens=2;mouche=2;passe=1;tuage++;scoreO++;bords();}
break;
case 3:i=premier[0];j=premier[1]+1;
if(monpay[j][i]==0){monpay2[j][i]=1;rondeInterd[r]=3;r++;}
if(monpay[j][i]>0){monpay2[j][i]=2;deux[0]=i;deux[1]=j;sens=3;mouche=2;passe=1;tuage++;scoreO++;bords();}
break;
case 4:i=premier[0]-1;j=premier[1];
if(monpay[j][i]==0){monpay2[j][i]=1;rondeInterd[r]=4;r++;}
if(monpay[j][i]>0){monpay2[j][i]=2;deux[0]=i;deux[1]=j;sens=4;mouche=2;passe=1;tuage++;scoreO++;bords();}
break;}
} //mouche =1
if(mouche==0)
{
tuage=0;
srand((unsigned)time(NULL));
do
{
i=(rand()%10);
j=(rand()%10);
}
while(monpay2[j][i]!=0);
if(monpay[j][i]==0){mouche=0;monpay2[j][i]=1;}
if(monpay[j][i]>0)
{
scoreO++;
tuage++;
mouche=1;
histo=0;
r=0;
premier[0]=i;premier[1]=j;//mise en mémoire du premier coup qui a fait mouche
monpay2[j][i]=2;}
}//mouche==0
freshCaseG(hwnd,i,j);
tour=0;
freshTorp(hwnd);
if(scoreO==20)
{
finS=1;
messageZ(hwnd,"La misérable machine a gagné");
freshTorp(hwnd);
return;
}
}//tour=1
return;
}//fin de la fonction combatsolo
void bords() //sous fonction de combatsolo pour éviter de tirer à cote du bato,
// ou il ne doit rien y avoir
{
if((sens%2)==0)//horizontal
{
if((monpay2[(premier[1]+1)][(premier[0])])==0)monpay2[(premier[1]+1)][(premier[0])]=3;//pour pas frapper juste a cote du bato
if((monpay2[(premier[1]-1)][(premier[0])])==0)monpay2[(premier[1]-1)][(premier[0])]=3;
if((monpay2[(deux[1]+1)][(deux[0])])==0)monpay2[(deux[1]+1)][(deux[0])]=3;
if((monpay2[(deux[1]-1)][(deux[0])])==0)monpay2[(deux[1]-1)][(deux[0])]=3;
}
if((sens%2)!=0)//vertical
{
if((monpay2[(premier[1])][(premier[0]+1)])==0)monpay2[(premier[1])][(premier[0]+1)]=3;//pour pas frapper juste a cote du bato
if((monpay2[(premier[1])][(premier[0]-1)])==0)monpay2[(premier[1])][(premier[0]-1)]=3;
if((monpay2[(deux[1])][(deux[0]+1)])==0)monpay2[(deux[1])][(deux[0]+1)]=3;
if((monpay2[(deux[1])][(deux[0]-1)])==0)monpay2[(deux[1])][(deux[0]-1)]=3;
}
}
void bords2()
{
if((sens%2)==0)//horizontal
{
if((monpay2[(deux[1]+1)][(deux[0])])==0)monpay2[(deux[1]+1)][(deux[0])]=3;
if((monpay2[(deux[1]-1)][(deux[0])])==0)monpay2[(deux[1]-1)][(deux[0])]=3;
}
if((sens%2)!=0)//vertical
{
if((monpay2[(deux[1])][(deux[0]+1)])==0)monpay2[(deux[1])][(deux[0]+1)]=3;
if((monpay2[(deux[1])][(deux[0]-1)])==0)monpay2[(deux[1])][(deux[0]-1)]=3;
}
}
BOOL optimiz() //sous fonction de combatsolo. ex : si 4 coup tirés et bato de 5
//déjà coulé, on peut cesser de tirer
//si vrai, c'est que pas de coup a tirer
//on remplace la case où on allait tirer par 3
{
int x;
//nb de bato coulés à 3 cases, 4 ...
int n2=0;
int n3=0;
int n4=0;
int n5=0;
for(x=0;x<6;x++)
{
if(tues[x]==2)n2++;
if(tues[x]==3)n3++;
if(tues[x]==4)n4++;
if(tues[x]==5)n5++;
}
if((tuage==3)&&(n4==1)&&(n5==1))return TRUE; //bato de 3
if((tuage==4)&&(n5==1))return TRUE; //bato de 4
if(tuage==5)return TRUE; //bato de 5
return FALSE;
}
void reinit()//pour relancer unepartie
{
int i,j;
for(j=0;j<10;j++)
for(i=0;i<10;i++)
{
ennemi[j][i]=0;
ennemi2[j][i]=0;
monpay[j][i]=0;
monpay2[j][i]=0;
monpay3[j][i]=0;
}
x=0;
y=0;//pour placement des bato enemi
vert=0;
//pour combat
/*
char essai[200];
char string1[35];
char string2[35];
char string3[35];
char string4[35];*/
Longueur=0;
k=0;
solo=0;
reso=0;
combatS=0;
tour=0;
mouche=0;
r=0;
histo=0;
//rondeInterd[4]={0,0,0,0};
passe=0;
t=0;
scoreJ=0;
scoreO=0;
finS=0;
choiB1=0; choiB5=0; choiB7=0; choiB3=0; choiB11=0; choiB9=0;
choiSens=0;
pose=0;
fix=0;
//réseau
if(keep==0)
{
serv=0;
cli=0;
soc=0;
}
combatR=0;
finR=0;
montour=0;
moiready=0;
luiready=0;
scoreOan=0;//pour faire un son quand ordi touche en solo.
scoreJan=0;
scoreR=0;//drapeau score reseau
aff=0;//id pour solo
sourFlag=1;
//pour midi
//mciSendCommand(wDeviceID, MCI_CLOSE, 0, 0);
flagson=0;
return;
}
// -------------------------------------------------------------------------
//
//
// FONCTIONS RESEAU
//
//
// --------------------------------------------------------------------------
void initsock()
{
if(WSAStartup(0X0101,&wsaData))
ShoErr("WSAStartup",WSAGetLastError());
else soc=1;
}
int verif()
{
if (soc==0){MessageBox(NULL,"WSAStartup non efectué","Erreur",MB_OK|MB_ICONSTOP);return 0;}
else return 1;
}
void nomhote()
{
char noms[10][50]; //tableau pour les noms d'hote de 10 lignes
char addr[10][20]; // pour les adresses
char big[600]; //gros buffer pour affichage final 400+200de trucs divers
int count=0;
int x=0;
int maxNom=0;
int maxAddr=0;
if (verif()==1)
{
if(gethostname(name,50)==SOCKET_ERROR ){ShoErr("gethostname",WSAGetLastError());return;}
host=(gethostbyname(name));
if(!host){ShoErr("gethostbyname",WSAGetLastError());return;}
count=0;
strcpy(noms[count],host->h_name);
count++;
maxNom++;
while(host->h_aliases[x]!=NULL)
{
strcpy(noms[count],host->h_aliases[count]);
x++;
maxNom++;
}
count=0;
while(host->h_addr_list[count])
{
ia.S_un.S_addr=*((LONG*)host->h_addr_list[count]);
strcpy(addr[count],inet_ntoa(ia));
count++;
maxAddr++;
}
count=0;
//mise en page
strcpy(big,"Renseignements concernant votre PC \nà communiquer à l'autre joueur : \n\n\nVotre adresse sur le réseau :\n\n");
strcat(big,"\tnom : \t\t");
strcat(big,noms[0]);
strcat(big,"\n");
if(maxNom>=2)
{
for(count=1;count<=maxNom;count++)
{
strcat(big,"\talias : \t\t");
strcat(big,noms[count]);
strcat(big,"\n");
}//for
}//if
for(count=0;count<maxAddr;count++)
{
strcat(big,"\tadresse : \t\t");
strcat(big,addr[count]);
strcat(big,"\n");
if((maxAddr>1)&&(count<1))strcat(big,"\n\nCette adresse est probablement votre\nadresse sur internet :\n\n");
}//for
MessageBox(NULL,big,"Renseignements sur l'ordinateur",MB_OK|MB_ICONINFORMATION);
}//fin du "if (verif()==1) "
else return;
}
void ShoErr(LPSTR string,int err)//ex: si erreur à bind, envoyerShoErr("Erreur à bind()",wsagetlasterror())
{
int i;
int max;
strcpy(bufbug,"Erreur ");
strcat(bufbug,string);
strcat(bufbug,"\n\n");
max=sizeof(Erreur)/sizeof(Erreur[0]);
for(i=0;i<max;i++)
if(err==Erreur[i].nErr){strcat(bufbug,Erreur[i].sErr);MessageBox(NULL,bufbug,"Erreur",MB_OK|MB_ICONSTOP);return;}
strcat(bufbug,"Erreur de type inconnu.");
MessageBox(NULL,bufbug,"Erreur",MB_OK|MB_ICONSTOP);
MessageBox(NULL,"Une erreur est apparue dans la connexion.\nLe jeu va être réinitialisé.","Réinitialisation",MB_OK);
reinit();
cleanNet();
return;
}
BOOL serveur(HWND hDlg,HWND hwnd)
{
serv=1;cli=0;
Sser=socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
if(Sser==INVALID_SOCKET)
{ShoErr("socket",WSAGetLastError());return FALSE;}
SetDlgItemText(hDlg,IDC_SERV,"socket créee");
if(SOCKET_ERROR==WSAAsyncSelect (Sser,hwnd, WM_SOCKET,FD_ACCEPT|FD_CLOSE))
{
ShoErr("WSAAsyncSelect",WSAGetLastError());
closesocket(Sser);
return FALSE;
}
sockAddr.sin_family=AF_INET;
sockAddr.sin_port=htons(5001);
sockAddr.sin_addr.s_addr=INADDR_ANY;
SetDlgItemText(hDlg,IDC_SERV,"socket initialisée");
if(bind(Sser,(LPSOCKADDR)&sockAddr,sizeof(sockAddr))==SOCKET_ERROR)
{ShoErr("bind",WSAGetLastError());return FALSE;}
SetDlgItemText(hDlg,IDC_SERV,"bind");
if(listen(Sser,1)==SOCKET_ERROR)
{ShoErr("listen",WSAGetLastError());closesocket(Sser);return FALSE;}
SetDlgItemText(hDlg,IDC_SERV,"Attente d'une connexion...");
return TRUE;
}
BOOL client(HWND hDlg,HWND hwnd)
{
cli=1;serv=0;
Scli=socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
if(Scli==INVALID_SOCKET)
{ShoErr("socket",WSAGetLastError());return FALSE;}
SetDlgItemText(hDlg,IDC_CLIE,"socket créeé");
if(SOCKET_ERROR==WSAAsyncSelect (Scli,hwnd, WM_SOCKET,FD_CONNECT|FD_READ|FD_WRITE|FD_CLOSE))
{
ShoErr("WSAAsyncSelect",WSAGetLastError());
closesocket(Scli);
return FALSE;
}
SetDlgItemText(hDlg,IDC_CLIE,"WSAAsybncselect");
sockAddr.sin_family=AF_INET;
sockAddr.sin_port=htons(5001);
//sockAddr.sin_addr.s_addr=inet_addr(szIPAddr);
connect(Scli,(SOCKADDR*)&sockAddr,sizeof(sockAddr));
if(WSAEWOULDBLOCK!=WSAGetLastError())
{ShoErr("connect",WSAGetLastError());closesocket(Scli);return FALSE;}
SetDlgItemText(hDlg,IDC_CLIE,"connect");
SetDlgItemText(hDlg,IDC_CLIE,"Client en place.Attente transfert données.");
//affiche(hwnd,"fonction client mise en place");
return TRUE;
}
BOOL liAddr()//pour lire l'adresse entrée par le client
//la chaine est récupérée dans idok
{
char error[20];
//szIPAddr
memset(&sockAddr,0,sizeof(sockAddr));
//si adree=sse à points :
sockAddr.sin_addr.s_addr=inet_addr(szIPAddr);
//si erreur de conversion, tentative avec chaine
if(sockAddr.sin_addr.s_addr==INADDR_NONE)
{
he=gethostbyname(szIPAddr);
if(he==NULL){ShoErr("gethostbyname",WSAGetLastError());}
if(he)
{
sockAddr.sin_addr.s_addr=*((DWORD*)he->h_addr);
return TRUE;
}
return FALSE;
}//fin INADDR NONe
return TRUE;
}
void cleanNet()
{
/*if((soc==1)&&(Sser!=INVALID_SOCKET))
{*/
shutdown(Sser,1);
closesocket(Sser);
/* }
if((soc==1)&&(Sinv!=INVALID_SOCKET))
{*/
shutdown(Sinv,1);
closesocket(Sinv);
/* }
if((soc==1)&&(Scli!=INVALID_SOCKET))
{*/
shutdown(Scli,1);
closesocket(Scli);
/* }*/
if(soc==1) { WSACleanup();soc=0;}
return;
}
void Angriff(HWND hwnd)//procédure de jeu en réseau
{
int n=0;
int i,j;
int a,xb,yb;
//lit facteur, voit quels sont ses drapeaux allumés
//attendre le resultat d'une attaque et l'attaque de l'aversaire avant de permettre la riposte
//pôur chaque ss fct, remise à 0 des autres quand fini
if(in[110]==1)//l'adversaire a posé tous ses bato
{
luiready=1;
messageZ(hwnd,"Votre adversaire a placé tous ses bateaux");
if((moiready==1)&&(luiready==1)&&(serv==1))choiprem(hwnd);//choix du premier par le serveur
//possibilité attaque
return;
}
//111 et 112 sont destiné au client seul. si 111, c'est le serveur qui commence (donc l'autre). Si 112, c'est lui qui commence
if(in[111]==1)//le serveur commence la partie
{
messageZ(hwnd,"Un tirage au sort désigne votre adversaire pour commencer la partie.");
montour=0;
freshTorp(hwnd);
combatR=1;
return;
}
if(in[112]==1)//le client commence la partie
{
messageZ(hwnd,"Vous avez été désigné par tirage au sort pour commencer la partie.");
montour=1;
freshTorp(hwnd);
combatR=1;
return;
}
if(in[115]==1)//retour résultat de notre attaque en 116 1-coup ds l'eau 2-mouche
//117 X 118 Y
{
xb=in[117];
yb=in[118];
a=in[116];
ennemi2[yb][xb]=a;
freshCaseD(hwnd,xb,yb);
if((a==1)&&(son==1))PlaySound("PLOUF.WAV",NULL,SND_RESOURCE |SND_ASYNC );
if((a==2)&&(son==1))PlaySound("EXPLO.WAV",NULL,SND_RESOURCE |SND_ASYNC );
//controle qui a gagné
for(j=0;j<10;j++)
for(i=0;i<10;i++)
if(ennemi2[j][i]==2)n++;
for(i=0;i<150;i++)buffer[i]=0;
if(n==20){buffer[145]=1;finR=1;MessageBox(hwnd,"Vous avez gagné","Gewinner",MB_OK);scoreR=1;InvalidateRect(hwnd,NULL,TRUE);UpdateWindow(hwnd);}
if(n<20)buffer[130]=1;
if(serv==1){iSendLen=send(Sinv,buffer,sizeof(buffer),0);if(iSendLen==0){ShoErr("send",WSAGetLastError());return ;}}//warzaza
if(cli==1){iSendLen=send(Scli,buffer,sizeof(buffer),0);if(iSendLen==0){ShoErr("send",WSAGetLastError());return ;}}
return;
}
if(in[120]==1)//attaque de l'adversaire X en 121 Y en 122
{
xb=in[121];
yb=in[122];
if(monpay[yb][xb]==0){a=1;monpay2[yb][xb]=1;if(son==1)PlaySound("PLOUF.WAV",NULL,SND_RESOURCE |SND_ASYNC );}
if(monpay[yb][xb]>0){a=2;monpay2[yb][xb]=2;if(son==1)PlaySound("EXPLO.WAV",NULL,SND_RESOURCE |SND_ASYNC );}
freshCaseG(hwnd,xb,yb);
//envoi resultat
for(i=0;i<150;i++)buffer[i]=0;
buffer[115]=1;
buffer[116]=a;
buffer[117]=xb;
buffer[118]=yb;
if(serv==1){iSendLen=send(Sinv,buffer,sizeof(buffer),0);if(iSendLen==0){ShoErr("send",WSAGetLastError());return ;}}//warzaza
if(cli==1){iSendLen=send(Scli,buffer,sizeof(buffer),0);if(iSendLen==0){ShoErr("send",WSAGetLastError());return ;}}
return;
}
if(in[130]==1)//après envoi attaque et réception résultat, chgt de tour
{
montour=1;
freshTorp(hwnd);
return;
}
if(in[135]==1)//nouveau jeu avec même adversaire en réseau
{
messageZ(hwnd,"Votre adversaire a souhaité reprendre une nouvelle partie");
keep=1;
reinit();
keep=0;
reso=1;
//messageZ(hwnd,"Sélectionnez un bateau du bouton gauche de la souris");
InvalidateRect(hwnd,NULL,TRUE);
UpdateWindow(hwnd);
return;
}
if(in[140]==1)//il s'agit d'un message
{
for(i=0;i<101;i++){bufmes[i]=in[i];}
messageZ(hwnd,bufmes);
//bruit
if(son==1)PlaySound("AOO-GAH.WAV",NULL,SND_RESOURCE |SND_ASYNC );
return;
}
if(in[145]==1)//un des joueurs a gagne.notification du gagnant
{
finR=1;
messageZ(hwnd,"Vous avez perdu.");
freshTorp(hwnd);
}
return;
}
void choiprem(HWND hwnd)//si j'arrive là, c'est que je suis le serveur
{
int i;
int a;
srand((unsigned)time(NULL));
do (i=1+(rand()%2));
while((i!=1)&&(i!=2));
if(i==1)//le serveur commence
{messageZ(hwnd,"Commencez le jeu en cliquant sur lagrille de votre ennemi");
for(a=0;a<150;a++)buffer[a]=0;buffer[111]=1;
iSendLen=send(Sinv, buffer, sizeof(buffer),0);
if(iSendLen==0){ShoErr("send",WSAGetLastError());return ;}
montour=1;freshTorp(hwnd);}
if(i==2)//l'autre commence
{for(a=0;a<150;a++)buffer[a]=0;buffer[112]=1;
iSendLen=send(Sinv, buffer, sizeof(buffer),0);
if(iSendLen==0){ShoErr("send",WSAGetLastError());return ;}
montour=0;freshTorp(hwnd);}
combatR=1;
return;
}
// Plays a specified MIDI file by using MCI_OPEN and MCI_PLAY. Returns
// as soon as playback begins. The window procedure function for the
// specified window will be notified when playback is complete.
// Returns 0L on success; otherwise, it returns an MCI error code.
DWORD playMIDIFile(HWND hWndNotify, LPSTR lpszMIDIFileName)
{
DWORD dwReturn;
MCI_OPEN_PARMS mciOpenParms;
MCI_STATUS_PARMS mciStatusParms;
// MCI_SEQ_SET_PARMS mciSeqSetParms;
// Open the device by specifying the device and filename.
// MCI will attempt to choose the MIDI mapper as the output port.
mciOpenParms.lpstrDeviceType = "sequencer";
mciOpenParms.lpstrElementName = lpszMIDIFileName;
if (dwReturn = mciSendCommand(0, MCI_OPEN,
MCI_OPEN_TYPE | MCI_OPEN_ELEMENT,
(DWORD)(LPVOID) &mciOpenParms))
{
// Failed to open device. Don't close it; just return error.
return (dwReturn);
}
// The device opened successfully; get the device ID.
wDeviceID = mciOpenParms.wDeviceID;
// Check if the output port is the MIDI mapper.
mciStatusParms.dwItem = MCI_SEQ_STATUS_PORT;
if (dwReturn = mciSendCommand(wDeviceID, MCI_STATUS,
MCI_STATUS_ITEM, (DWORD)(LPVOID) &mciStatusParms))
{
mciSendCommand(wDeviceID, MCI_CLOSE, 0, 0);
flagson=0;
return (dwReturn);
}
// The output port is not the MIDI mapper.
// Ask if the user wants to continue.
//position curseur
mciSendCommand(wDeviceID,MCI_SEEK,MCI_SEEK_TO_START,0);
// Begin playback. The window procedure function for the parent
// window will be notified with an MM_MCINOTIFY message when
// playback is complete.
mciPlayParms.dwCallback = (DWORD) hWndNotify;
if (dwReturn = mciSendCommand(wDeviceID, MCI_PLAY, MCI_NOTIFY,
(DWORD)(LPVOID) &mciPlayParms))
{
mciSendCommand(wDeviceID, MCI_CLOSE, 0, 0);
flagson=0;
return (dwReturn);
}
return (0L);
}
Conclusion :
l'executable peut être téléchargé à
http://www.logitheque.com/fiche.asp?I=9095
Vous n'êtes pas encore membre ?
inscrivez-vous, c'est gratuit et ça prend moins d'une minute !
Les membres obtiennent plus de réponses que les utilisateurs anonymes.
Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.
Le fait d'être membre vous permet d'avoir des options supplémentaires.