@echo off

:globals
set game=3-5-7 Dot Game
set name1=
set name2=
set nextplayer=1
set tdisplay=O O O
set mdisplay=O O O O O
set bdisplay=O O O O O O O
set t=3
set m=5
set b=7

:welcome
  title %game%
  cls
   echo.
   echo  Welcome to %game%.
   echo.
   echo  Press any key to continue to the next page.
   echo.
   pause > nul
   goto names
  
:names
  title %game% - Names
  cls
   echo.
   echo  Please enter your names below.
   echo.
   if not defined name1 (
   set /p "name1= Player 1: "
   goto names
   )
   if not defined name2 (
   set /p "name2= Player 2: "
   )
   if not defined name2 goto names
   goto startgame
  
:startgame
  mode con cols=40 lines=11
  title %game% - Player %nextplayer%'s turn.
  cls
   if %nextplayer% EQU 1 (
   echo  ------------ %name1%'s Turn ------------
   )
   if %nextplayer% EQU 2 (
   echo  ------------ %name2%'s Turn ------------
   )
   echo.
   echo           [Type RETYPE to redo]
   echo.
   echo.
   echo     T -^>  %tdisplay%
   echo     M -^>  %mdisplay%
   echo     B -^>  %bdisplay%
   echo.
   echo.
   if not defined layerchoice (
   set /p "layerchoice= Enter T,M, or B: "
   goto checklayerchoice
   )
   if not defined number (
   set /p "number= Enter a number to remove: "
   goto numbercheck
   )
  
:checklayerchoice
if not defined layerchoice goto startgame
if /i "%layerchoice%" EQU "retype" set "layerchoice=" & goto startgame
if /i "%layerchoice%" EQU "t" call :tlayernumcheck & goto startgame
if /i "%layerchoice%" EQU "m" call :mlayernumcheck & goto startgame
if /i "%layerchoice%" EQU "b" call :blayernumcheck & goto startgame
set "layerchoice=" & goto startgame

   :tlayernumcheck
       if /i %t% EQU 0 (
    echo This row is empty. Press any key to try again.
    pause > nul
    set "layerchoice="
    goto startgame
    )
    goto :eof
   :mlayernumcheck
       if /i %m% EQU 0 (
    echo This row is empty. Press any key to try again.
    pause > nul
    set "layerchoice="
    goto startgame
    )
    goto :eof
   :blayernumcheck
       if /i %b% EQU 0 (
    echo This row is empty. Press any key to try again.
    pause > nul
    set "layerchoice="
    goto startgame
    )
    goto :eof

:numbercheck
if not defined number goto startgame
if %number% EQU 0 set "number=" & goto startgame
if /i "%number%" EQU "retype" set "layerchoice=" & set "number=" & goto startgame
if /i "%layerchoice%" EQU "t" call :tnumcheck & goto finalize
if /i "%layerchoice%" EQU "m" call :mnumcheck & goto finalize
if /i "%layerchoice%" EQU "b" call :bnumcheck & goto finalize
   :tnumcheck
     if %number% GTR %t% (
  echo You mush enter a smaller number. Press any key to retry.
  pause > nul
  set "number=" & goto startgame
  )
  set /a t -=%number%
  goto :eof
   :mnumcheck
    if %number% GTR %m% (
  echo You mush enter a smaller number. Press any key to retry.
  pause > nul
  set number=
  goto startgame
  )
  set /a m -=%number%
  goto :eof
   :bnumcheck
    if %number% GTR %b% (
  echo You mush enter a smaller number. Press any key to retry.
  pause > nul
  set "number=" & goto startgame
  )
  set /a b -=%number%
  goto :eof
 
:finalize
   :tfinalize
      if %t% EQU 0 set "tdisplay= "
   if %t% EQU 1 set "tdisplay=O"
   if %t% EQU 2 set "tdisplay=O O"
   if %t% EQU 3 set "tdisplay=O O O"
   :mfinalize
      if %m% EQU 0 set "mdisplay= "
   if %m% EQU 1 set "mdisplay=O"
   if %m% EQU 2 set "mdisplay=O O"
   if %m% EQU 3 set "mdisplay=O O O"
   if %m% EQU 4 set "mdisplay=O O O O"
   if %m% EQU 5 set "mdisplay=O O O O O"
   :bfinalize
      if %b% EQU 0 set "bdisplay= "
   if %b% EQU 1 set "bdisplay=O"
   if %b% EQU 2 set "bdisplay=O O"
   if %b% EQU 3 set "bdisplay=O O O"
   if %b% EQU 4 set "bdisplay=O O O O"
   if %b% EQU 5 set "bdisplay=O O O O O"
   if %b% EQU 6 set "bdisplay=O O O O O O"
   if %b% EQU 7 set "bdisplay=O O O O O O O"
set number=
set layerchoice=
call :gamewin
   if %nextplayer% EQU 1 (
   set nextplayer=2
   goto startgame
   )
   if %nextplayer% EQU 2 (
   set nextplayer=1
   goto startgame
   )
  
:gamewin
   if %t% NEQ 0 goto :eof
   if %m% NEQ 0 goto :eof
   if %b% NEQ 0 goto :eof
   goto finish
  
:finish
  title %game% - Game Over
  cls
  echo.
  if %nextplayer% EQU 1 (
  echo %name1% Wins!
  )
  if %nextplayer% EQU 2 (
  echo %name2% Wins!
  )
  echo.
  echo.
  pause
  goto globals