void netGameEnd() { if(!isNetGame){ if(isListening) stopListen(); return; } wr1(C_END); undoRequest=0; for(int i=0; i<50; i++){ Sleep(200); if(!isNetGame) return; } EnterCriticalSection(&netLock); if(netGameThread){ wrLog("The other computer is not responding"); netGameDone(); TerminateThread(netGameThread, 1); } LeaveCriticalSection(&netLock); }
DWORD WINAPI listenNetGame(void *) { sockaddr_in sa; SOCKET sock_c; int sl; bool b; wrLog(lng(866, "Waiting for the other player")); for(;;){ sl= sizeof(sa); if((sock_c= accept(sock_l, (sockaddr *)&sa, &sl))==INVALID_SOCKET){ wrLog(lng(867, "Finished listening for requests from the internet")); break; } EnterCriticalSection(&netLock); b=isNetGame; isNetGame=true; LeaveCriticalSection(&netLock); if(b){ wr1(sock_c, C_BUSY); Sleep(1000); closesocket(sock_c); } else{ sock=sock_c; netGameIP=sa; initWSA(); DWORD id; ResumeThread(netGameThread=CreateThread(0, 0, netGameLoop, 0, CREATE_SUSPENDED, &id)); stopListen(); break; } } WSACleanup(); isListening=false; return 0; }
DWORD WINAPI netGameLoop(void *param) { int len, c, x, y; Psquare p; TnetGameSettings *b; char *m, *u, *a; hostent *h; char buf[256]; if(!param){ buf[0]=(BYTE)C_INIT1; buf[1]=NETGAME_VERSION; wr(buf, 2); c=rd1(); if(c!=C_INIT2) goto le; c=rd1(); if(c<1) goto le; netGameVersion=c; SetForegroundWindow(hWin); h= gethostbyaddr((char*)&netGameIP.sin_addr, 4, netGameIP.sin_family); a= inet_ntoa(netGameIP.sin_addr); if(msg1(MB_YESNO|MB_ICONQUESTION, lng(868, "Do you want to play with %s [%s] ?"), (h && h->h_name) ? h->h_name : "???", a ? a : "???")!=IDYES){ wr1(C_INIT_DENY); goto le; } buf[0]=(BYTE)C_INFO; buf[1]=sizeof(TnetGameSettings); b= (TnetGameSettings*)(buf+2); b->width=(char)width; b->height=(char)height; b->begin= (player==1); b->rule5=(char)ruleFive; b->cont=(char)continuous; if(netGameVersion<2) b->rule5=b->cont=0; wr(buf, 2+sizeof(TnetGameSettings)); if(rd1()!=C_INFO_OK) goto le; b->begin= !b->begin; } else{ buf[0]=(BYTE)C_INIT2; buf[1]=NETGAME_VERSION; wr(buf, 2); c=rd1(); if(c==C_BUSY) wrLog(lng(874, "The other player is already playing with someone else")); if(c!=C_INIT1) goto le; c=rd1(); if(c<1) goto le; netGameVersion=c; wrLog(lng(871, "Connection established. Waiting for response...")); c=rd1(); if(c==C_INIT_DENY) wrLog(lng(870, "The other player doesn't want to play with you !")); if(c!=C_INFO) goto le; len=rd1(); b= (TnetGameSettings*)buf; memset(buf, 0, sizeof(TnetGameSettings)); if(len<2 || rd(buf, len)<0) goto le; wr1(C_INFO_OK); } SendMessage(hWin, WM_COMMAND, 992, (LPARAM)b); undoRequest=0; for(;;){ x=rd1(); switch(x){ case C_MSG: //message len=rd2(); if(len<=0) goto le; u=new char[2*len]; if(rd(u, 2*len)>=0){ m=new char[len+1]; WideCharToMultiByte(CP_ACP, 0, (WCHAR*)u, len, m, len, 0, 0); m[len]='\0'; wrLog("---> %s", m); delete[] m; SetWindowPos(logDlg, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE|SWP_ASYNCWINDOWPOS|SWP_NOACTIVATE|SWP_SHOWWINDOW); } delete[] u; break; case C_UNDO_REQUEST: y=rd2(); if(y<=0 || y>moves) goto le; if(undoRequest){ //both players pressed undo simultaneously if(undoRequest<0) undoRequest=y; amax(undoRequest, y); postUndo(); } else{ c=msg1(MB_YESNO|MB_ICONQUESTION, lng(876, "The other player wants to UNDO the last move.\r\nDo you agree ?")); if(c==IDYES){ undoRequest=y; wr1(C_UNDO_YES); postUndo(); } else{ wr1(C_UNDO_NO); } } break; case C_NEW_REQUEST: if(undoRequest){ if(undoRequest<0){ //both players pressed NewGame simultaneously postNewGame(); } else{ postUndo(); } } else{ c=msg1(MB_YESNO|MB_ICONQUESTION, lng(877, "The other player wants to start a NEW game.\r\nDo you agree ?")); if(c==IDYES){ undoRequest=-1; wr1(C_NEW_YES); postNewGame(); } else{ wr1(C_NEW_NO); } } break; case C_UNDO_YES: if(undoRequest<=0) goto le; postUndo(); break; case C_UNDO_NO: if(undoRequest>0){ msglng(878, "Sorry, the other player does not allow to UNDO your move."); undoRequest=0; } break; case C_NEW_YES: if(undoRequest>=0) goto le; postNewGame(); break; case C_NEW_NO: if(undoRequest<0){ msglng(879, "Sorry, the other player does not allow to start a NEW game."); undoRequest=0; } break; default: //move or error if(x<0 || x>=width){ show(logDlg); goto le; } while(finished && (getTickCount()-lastTick<5000 || saveLock)){ Sleep(200); } if(finished) SendMessage(hWin, WM_COMMAND, 101, 0); y=rd1(); if(y<0 || y>=height) goto le; p=Square(x, y); p->time=rd4(); PostMessage(hWin, WM_COMMAND, 991, (LPARAM)p); } } le: EnterCriticalSection(&netLock); netGameDone(); LeaveCriticalSection(&netLock); return 0; }
static void testnsevdproblem(const ap::real_2d_array& a, int n, double& vecerr, double& valonlydiff, bool& wfailed) { double mx; int i; int j; int k; int vjob; bool needl; bool needr; ap::real_1d_array wr0; ap::real_1d_array wi0; ap::real_1d_array wr1; ap::real_1d_array wi1; ap::real_1d_array wr0s; ap::real_1d_array wi0s; ap::real_1d_array wr1s; ap::real_1d_array wi1s; ap::real_2d_array vl; ap::real_2d_array vr; ap::real_1d_array vec1r; ap::real_1d_array vec1i; ap::real_1d_array vec2r; ap::real_1d_array vec2i; ap::real_1d_array vec3r; ap::real_1d_array vec3i; double curwr; double curwi; double vt; double tmp; vec1r.setbounds(0, n-1); vec2r.setbounds(0, n-1); vec3r.setbounds(0, n-1); vec1i.setbounds(0, n-1); vec2i.setbounds(0, n-1); vec3i.setbounds(0, n-1); wr0s.setbounds(0, n-1); wr1s.setbounds(0, n-1); wi0s.setbounds(0, n-1); wi1s.setbounds(0, n-1); mx = 0; for(i = 0; i <= n-1; i++) { for(j = 0; j <= n-1; j++) { if( fabs(a(i,j))>mx ) { mx = fabs(a(i,j)); } } } if( mx==0 ) { mx = 1; } // // Load values-only // if( !rmatrixevd(a, n, 0, wr0, wi0, vl, vr) ) { wfailed = false; return; } // // Test different jobs // for(vjob = 1; vjob <= 3; vjob++) { needr = vjob==1||vjob==3; needl = vjob==2||vjob==3; if( !rmatrixevd(a, n, vjob, wr1, wi1, vl, vr) ) { wfailed = false; return; } // // Test values: // 1. sort by real part // 2. test // ap::vmove(&wr0s(0), &wr0(0), ap::vlen(0,n-1)); ap::vmove(&wi0s(0), &wi0(0), ap::vlen(0,n-1)); for(i = 0; i <= n-1; i++) { for(j = 0; j <= n-2-i; j++) { if( wr0s(j)>wr0s(j+1) ) { tmp = wr0s(j); wr0s(j) = wr0s(j+1); wr0s(j+1) = tmp; tmp = wi0s(j); wi0s(j) = wi0s(j+1); wi0s(j+1) = tmp; } } } ap::vmove(&wr1s(0), &wr1(0), ap::vlen(0,n-1)); ap::vmove(&wi1s(0), &wi1(0), ap::vlen(0,n-1)); for(i = 0; i <= n-1; i++) { for(j = 0; j <= n-2-i; j++) { if( wr1s(j)>wr1s(j+1) ) { tmp = wr1s(j); wr1s(j) = wr1s(j+1); wr1s(j+1) = tmp; tmp = wi1s(j); wi1s(j) = wi1s(j+1); wi1s(j+1) = tmp; } } } for(i = 0; i <= n-1; i++) { valonlydiff = ap::maxreal(valonlydiff, fabs(wr0s(i)-wr1s(i))); valonlydiff = ap::maxreal(valonlydiff, fabs(wi0s(i)-wi1s(i))); } // // Test right vectors // if( needr ) { k = 0; while(k<=n-1) { if( wi1(k)==0 ) { ap::vmove(vec1r.getvector(0, n-1), vr.getcolumn(k, 0, n-1)); for(i = 0; i <= n-1; i++) { vec1i(i) = 0; } curwr = wr1(k); curwi = 0; } if( wi1(k)>0 ) { ap::vmove(vec1r.getvector(0, n-1), vr.getcolumn(k, 0, n-1)); ap::vmove(vec1i.getvector(0, n-1), vr.getcolumn(k+1, 0, n-1)); curwr = wr1(k); curwi = wi1(k); } if( wi1(k)<0 ) { ap::vmove(vec1r.getvector(0, n-1), vr.getcolumn(k-1, 0, n-1)); ap::vmoveneg(vec1i.getvector(0, n-1), vr.getcolumn(k, 0, n-1)); curwr = wr1(k); curwi = wi1(k); } for(i = 0; i <= n-1; i++) { vt = ap::vdotproduct(&a(i, 0), &vec1r(0), ap::vlen(0,n-1)); vec2r(i) = vt; vt = ap::vdotproduct(&a(i, 0), &vec1i(0), ap::vlen(0,n-1)); vec2i(i) = vt; } ap::vmove(&vec3r(0), &vec1r(0), ap::vlen(0,n-1), curwr); ap::vsub(&vec3r(0), &vec1i(0), ap::vlen(0,n-1), curwi); ap::vmove(&vec3i(0), &vec1r(0), ap::vlen(0,n-1), curwi); ap::vadd(&vec3i(0), &vec1i(0), ap::vlen(0,n-1), curwr); for(i = 0; i <= n-1; i++) { vecerr = ap::maxreal(vecerr, fabs(vec2r(i)-vec3r(i))); vecerr = ap::maxreal(vecerr, fabs(vec2i(i)-vec3i(i))); } k = k+1; } } // // Test left vectors // if( needl ) { k = 0; while(k<=n-1) { if( wi1(k)==0 ) { ap::vmove(vec1r.getvector(0, n-1), vl.getcolumn(k, 0, n-1)); for(i = 0; i <= n-1; i++) { vec1i(i) = 0; } curwr = wr1(k); curwi = 0; } if( wi1(k)>0 ) { ap::vmove(vec1r.getvector(0, n-1), vl.getcolumn(k, 0, n-1)); ap::vmove(vec1i.getvector(0, n-1), vl.getcolumn(k+1, 0, n-1)); curwr = wr1(k); curwi = wi1(k); } if( wi1(k)<0 ) { ap::vmove(vec1r.getvector(0, n-1), vl.getcolumn(k-1, 0, n-1)); ap::vmoveneg(vec1i.getvector(0, n-1), vl.getcolumn(k, 0, n-1)); curwr = wr1(k); curwi = wi1(k); } for(j = 0; j <= n-1; j++) { vt = ap::vdotproduct(vec1r.getvector(0, n-1), a.getcolumn(j, 0, n-1)); vec2r(j) = vt; vt = ap::vdotproduct(vec1i.getvector(0, n-1), a.getcolumn(j, 0, n-1)); vec2i(j) = -vt; } ap::vmove(&vec3r(0), &vec1r(0), ap::vlen(0,n-1), curwr); ap::vadd(&vec3r(0), &vec1i(0), ap::vlen(0,n-1), curwi); ap::vmove(&vec3i(0), &vec1r(0), ap::vlen(0,n-1), curwi); ap::vsub(&vec3i(0), &vec1i(0), ap::vlen(0,n-1), curwr); for(i = 0; i <= n-1; i++) { vecerr = ap::maxreal(vecerr, fabs(vec2r(i)-vec3r(i))); vecerr = ap::maxreal(vecerr, fabs(vec2i(i)-vec3i(i))); } k = k+1; } } } }